|
|
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 theclasskeyword. In the following class declaration, the class name isPoint:At runtime, 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 on 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
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.