Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 2 of 6
ClassInfoDemo2 Employee char [C
ClassInfoDemo2 and Employee are the names of ClassInfoDemo2's reference types, whereas char is the name of a primitive type. But what is [C? The open square bracket signifies a one-dimensional array. Furthermore, capital letter C signifies that each array element is of type character. (That rather strange notation originates with the JVM.)
Class declares various information-related methods. To get a taste of those methods, examine Class's getDeclaredField() and getSuperclass() methods below:
Listing 4. ClassInfoDemo3.java
// ClassInfoDemo3.java
import java.lang.reflect.Field;
class Employee
{
private String name;
Employee (String name)
{
this.name = name;
}
String getName ()
{
return name;
}
}
class Credentials
{
}
class Accountant extends Employee
{
private Credentials credentials;
Accountant (String name, Credentials credentials)
{
super (name);
this.credentials = credentials;
}
Credentials getCredentials ()
{
return credentials;
}
}
class ClassInfoDemo3
{
public static void main (String [] args)
{
Accountant jim = new Accountant ("Jim Smith", new Credentials ());
Class c = jim.getClass ();
do
{
try
{
Field f = c.getDeclaredField ("name");
System.out.println (f.getType ().getName ());
return;
}
catch (NoSuchFieldException e)
{
}
c = c.getSuperclass ();
}
while (c != null);
}
}
ClassInfoDemo3 is an involved program. It creates an Accountant object and returns the type name of Accountant's inherited name variable. To accomplish that task, the program first obtains the Class object associated with the Accountant object that jim references.
The program enters a loop and, using jim's Class object, attempts to obtain a Field object that describes name by calling Class's getDeclaredField() method. The getDeclaredField() method either returns a Field object describing the name field -- if Accountant declares name -- or throws an exception. (Note: Although ClassInfoDemo3 uses exception-handling code, I defer a discussion of exceptions and exception handling to a future article.) Because Accountant does not declare name, the method getDeclaredField() throws an exception of type NoSuchFieldException. A catch clause handles that exception -- by doing nothing more than allowing the program to continue. The code then calls Class's getSuperclass() method to return a reference to a Class object that corresponds to Accountant's superclass -- Employee.
The loop continues and makes a second call to getDeclaredClasses(). That method returns a reference to a Field object describing name -- because Employee declares name. Finally, a call to the Field object's getType() method returns a Class object describing the field's type; and a call to that Class object's getName() method returns the type's name, which subsequently prints as java.lang.String.
ClassInfoDemo3 uses some sophisticated language features: import directives (import java.lang.reflect.Field), packages (java.lang.reflect), exceptions and exception handling, and the Reflection API. Don't worry about grasping those features now -- you will learn
about them in future articles. For now, think about the getClass() method and what it allows you to accomplish.