Examining Classes |
Every class in the Java programming language has a name. When you declare a class, the name immediately follows theclass
keyword. In the following class declaration, the class name isPoint
:At run time, you can determine the name of apublic class Point {int x, y;}Class
object by invoking thegetName
method. TheString
returned bygetName
is the fully-qualified name of the class.The following program gets the class name of an object. First, it retrieves the corresponding
Class
object, and then it invokes thegetName
method upon thatClass
object.The sample program prints the following line:import java.lang.reflect.*; import java.awt.*; class SampleName { public static void main(String[] args) { Button b = new Button(); printName(b); } static void printName(Object o) { Class c = o.getClass(); String s = c.getName(); System.out.println(s); } }java.awt.Button
Examining Classes |