At run time, you can determine the name of a
The following program gets the class name of an object. First, it retrieves the corresponding
Class
object by invoking the getName
method. The String
returned by getName
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 the getName
method upon that Class
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
No comments:
Post a Comment