Download the JDK
Search the Tutorials
Hide the TOC
Trail: The Reflection API
Lesson: Examining Classes
Examining Classes
Retrieving Class Objects
Getting the Class Name
Discovering Class Modifiers
Finding Superclasses
Identifying the Interfaces Implemented by a Class
Examining Interfaces
Identifying Class Fields
Discovering Class Constructors
Obtaining Method Information
Home Page > The Reflection API > Examining Classes
« Previous • Trail • Next »
Getting the Class Name

> Every class in the Java programming language has a name. When you declare a class, the name immediately follows the `class` keyword. In the following class declaration, the class name is `Point`:

> >     public class Point {int x, y;}

> At runtime, you can determine the name of a `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 on that `Class` object.

> >     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);
> >        }
> >     }

> The sample program prints the following line:

> >     java.awt.Button

« Previous • Trail • Next »
Previous page: Retrieving Class Objects
Next page: Discovering Class Modifiers