|
|
Examining Classes |
Every class in the Java programming language has a name. When you declare a class, the name immediately follows theclasskeyword. In the following class declaration, the class name isPoint:At run time, you can determine the name of apublic class Point {int x, y;}Classobject by invoking thegetNamemethod. TheStringreturned bygetNameis the fully-qualified name of the class.The following program gets the class name of an object. First, it retrieves the corresponding
Classobject, and then it invokes thegetNamemethod upon thatClassobject.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 |