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 »
Discovering Class Modifiers

> A class declaration may include the following modifiers: `public`, `abstract`, or `final`. The class modifiers precede the `class` keyword in the class definition. In the following example, the class modifiers are `public` and `final`:

> >     public final Coordinate {int x, int y, int z}

> To identify the modifiers of a class at runtime you perform these steps:
> 1. Invoke `getModifiers` on a `Class` object to retrieve a set of modifiers.
> 2. Check the modifiers by calling `isPublic`, `isAbstract`, and `isFinal`.
> The following program identifies the modifiers of the `String` class.

> >     import java.lang.reflect.*;
> >     import java.awt.*;

> >     class SampleModifier {

> >        public static void main(String[] args) {
> >           String s = new String();
> >           printModifiers(s);
> >        }

> >        public static void printModifiers(Object o) {
> >           Class c = o.getClass();
> >           int m = c.getModifiers();
> >           if (Modifier.isPublic(m))
> >              System.out.println("public");
> >           if (Modifier.isAbstract(m))
> >              System.out.println("abstract");
> >           if (Modifier.isFinal(m))
> >              System.out.println("final");
> >        }
> >     }

> The output of the sample program reveals that the modifiers of the `String` class are `public` and `final`:

> >     public
> >     final

« Previous • Trail • Next »
Previous page: Getting the Class Name
Next page: Finding Superclasses