Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
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 runtime, 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 on 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
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.