Class of the Month: The Reflection API

A number of popular Java technologies and frameworks depend on the Reflection API to function. Learn more about its many uses.

provided by: 
Originally published at Internet.com


Java classes are the main building blocks of a Java application. As developers, we write some classes, but we also use classes developed by others. We rely on the API listings and documentation to learn how to use these "third-party" classes effectively. That works well (at least as well as the quality of the documentation!) in development stages. Java also provides a framework for learning about classes during runtime. That framework is called Reflection and is wrapped as the java.lang.reflect package.

Piroz Mohseni

A number of popular Java technologies/frameworks depend on the Reflection API to function. For example, a JavaBean container uses the Reflection API to learn about the properties and methods of a Bean. Reflection is also used in Remote Method Invocation. With the introduction of Java/XML binding technologies like JAXB, Reflection may also be used to learn about the structure of an XML document by looking at a "class" representation of an XML document. The following example provides a simple explanation of how the Reflection API could be used.

Consider the sample class called Foo shown below. It has three fields: num, c, and test. It also has two constructors. Note that one of the constructors is non-public. It also has three methods, where one (methodB) is labeled as private. public class Foo { public int num; public char c; String test; public Foo() { num = 2; c = 'b'; test= "test"; System.out.println("Foo constructor"); } Foo(int i) { num = i; c = 'b'; test = "test"; System.out.println("Foo constructor with param"); } public void methodA() { System.out.println("Method A"); } private void methodB() { System.out.println("Method B"); } public int methodC(int i, char c) { System.out.println("i = " + i + " c= " + c); return i; } } ...

Read article at Internet.com site
Regional Articles
- Class of the Month: The Reflection API Alabama
- Class of the Month: The Reflection API Alaska
- Class of the Month: The Reflection API Arizona
- Class of the Month: The Reflection API Arkansas
- Class of the Month: The Reflection API California
- Class of the Month: The Reflection API Colorado
- Class of the Month: The Reflection API Connecticut
- Class of the Month: The Reflection API DC
- Class of the Month: The Reflection API Delaware
- Class of the Month: The Reflection API Florida
- Class of the Month: The Reflection API Georgia
- Class of the Month: The Reflection API Hawaii
- Class of the Month: The Reflection API Idaho
- Class of the Month: The Reflection API Illinois
- Class of the Month: The Reflection API Indiana
- Class of the Month: The Reflection API Iowa
- Class of the Month: The Reflection API Kansas
- Class of the Month: The Reflection API Kentucky
- Class of the Month: The Reflection API Louisiana
- Class of the Month: The Reflection API Maine
- Class of the Month: The Reflection API Maryland
- Class of the Month: The Reflection API Massachusetts
- Class of the Month: The Reflection API Michigan
- Class of the Month: The Reflection API Minnesota
- Class of the Month: The Reflection API Mississippi
- Class of the Month: The Reflection API Missouri
- Class of the Month: The Reflection API Montana
- Class of the Month: The Reflection API Nebraska
- Class of the Month: The Reflection API Nevada
- Class of the Month: The Reflection API New Hampshire
- Class of the Month: The Reflection API New Jersey
- Class of the Month: The Reflection API New Mexico
- Class of the Month: The Reflection API New York
- Class of the Month: The Reflection API North Carolina
- Class of the Month: The Reflection API North Dakota
- Class of the Month: The Reflection API Ohio
- Class of the Month: The Reflection API Oklahoma
- Class of the Month: The Reflection API Oregon
- Class of the Month: The Reflection API Pennsylvania
- Class of the Month: The Reflection API Rhode Island
- Class of the Month: The Reflection API South Carolina
- Class of the Month: The Reflection API South Dakota
- Class of the Month: The Reflection API Tennessee
- Class of the Month: The Reflection API Texas
- Class of the Month: The Reflection API Utah
- Class of the Month: The Reflection API Vermont
- Class of the Month: The Reflection API Virginia
- Class of the Month: The Reflection API Washington
- Class of the Month: The Reflection API West Virginia
- Class of the Month: The Reflection API Wisconsin
- Class of the Month: The Reflection API Wyoming

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Architecture & Design Languages & Tools Project Management Web Services
Database Microsoft & .NET Security Wireless
Java Open Source Techniques XML