Java Reference
In-Depth Information
public static void forName() {
try {
String className = "com.jdojo.reflection.Bulb";
// Will load and initialize the class
Class c = Class.forName(className);
}
catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
}
public static void createObject() {
// Will load and initialize the Bulb class
new Bulb();
}
}
Loading class Bulb...
Reflecting on a Class
This section will demonstrate the features of Java reflection that enable you to get the description of a class, such
as its package name, its access modifiers, etc. You will use a Person class as listed in Listing 3-3 to demonstrate the
reflection features. It is a simple class with two instance fields, two constructors, some methods, and it implements
two interfaces.
Listing 3-3. A Person Class Used to Demonstrate Reflection
// Person.java
package com.jdojo.reflection;
import java.io.Serializable;
public class Person implements Cloneable, Serializable {
private int id = -1;
private String name = "Unknown";
public Person() {
}
public Person(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
 
Search WWH ::




Custom Search