- Java Reflection Tutorial
- Java Reflection - Classes
- Java Reflection - Constructors
- Java Reflection - Fields
- Java Reflection - Methods
- Java Reflection - Getters and Setters
- Java Reflection - Private Fields and Methods
- Java Reflection - Annotations
- Java Reflection - Generics
- Java Reflection - Arrays
- Java Reflection - Dynamic Proxies
- Java Reflection - Dynamic Class Loading and Reloading
Java Reflection - Classes
Using Java Reflection you can inspect Java classes at runtime. Inspecting classes is often the first thing you do when using Reflection. From the classes you can obtain information about
- Class Name
- Class Modifies (public, private, synchronized etc.)
- Package Info
- Implemented Interfaces
plus a lot more information related to Java classes. For a full list you should consult the
JavaDoc for java.lang.Class.
This text will briefly touch upon all accessing of the above mentioned information. Some of the topics
will also be examined in greater detail in separate texts. For instance, this text will show you how
to obtain all methods or a specific method, but a separate text will show you how to invoke that method,
how to find the method matching a given set of arguments if more than one method exists with the same name,
what exceptions are thrown from method invocation via reflection, how to spot a getter/setter etc.
The purpose of this text is primarily to introduce the
Class object and the information
you can obtain from it.
The Class Object
Before you can do any inspection on a class you need to obtain its
All types in Java including the primitive types (int, long, float etc.) including arrays have an associated
If you know the name of the class at compile time you can obtain a
Class object like this:
Class myObjectClass = MyObject.class
If you don't know the name at compile time, but have the class name as a string at runtime, you can do like this:
String className = ... //obtain class name as string at runtime Class class = Class.forName(className);
When using the
Class.forName() method you must supply the fully qualified class name.
That is the class name including all package names. For instance, if
com.jenkov.myapp then the fully qualified class name is
Class.forName() method may throw a
the class cannot be found on the classpath at runtime.
Class object you can obtain its name in two versions. The fully qualified
class name (including package name) is obtained using the
getName() method like
Class aClass = ... //obtain Class object. See prev. section String className = aClass.getName();
If you want the class name without the pacakge name you can obtain it using the
method, like this:
Class aClass = ... //obtain Class object. See prev. section String simpleClassName = aClass.getSimpleName();
You can access the modifiers of a class via the
Class object. The class modifiers are the
keywords "public", "private", "static" etc. You obtain the class modifiers like this:
Class aClass = ... //obtain Class object. See prev. section int modifiers = aClass.getModifiers();
The modifiers are packed into an
int where each modifier is a flag bit that is either set
or cleared. You can check the modifiers using these methods in the class
Modifier.isAbstract(int modifiers) Modifier.isFinal(int modifiers) Modifier.isInterface(int modifiers) Modifier.isNative(int modifiers) Modifier.isPrivate(int modifiers) Modifier.isProtected(int modifiers) Modifier.isPublic(int modifiers) Modifier.isStatic(int modifiers) Modifier.isStrict(int modifiers) Modifier.isSynchronized(int modifiers) Modifier.isTransient(int modifiers) Modifier.isVolatile(int modifiers)
You can obtain information about the package from a
Class object like this:
Class aClass = ... //obtain Class object. See prev. section Package package = aClass.getPackage();
Package object you have access to information about the package
like its name. You can also access information specified for this package in the
Manifest file of the JAR file this package is located in on the classpath.
For instance, you can specify package version numbers in the
You can read more about the
Package class here:
Class object you can access the superclass of the class. Here is how:
Class superclass = aClass.getSuperclass();
The superclass class object is a
Class object like any other, so you can continue
doing class reflection on that too.
It is possible to get a list of the interfaces implemented by a given class. Here is how:
Class aClass = ... //obtain Class object. See prev. section Class interfaces = aClass.getInterfaces();
A class can implement many interfaces. Therefore an array of
Class is returned.
Interfaces are also represented by
Class objects in Java Reflection.
NOTE: Only the interfaces specifically declared implemented by a given class is returned. If a superclass of the class implements an interface, but the class doesn't specifically state that it also implements that interface, that interface will not be returned in the array. Even if the class in practice implements that interface, because the superclass does.
To get a complete list of the interfaces implemented by a given class you will have to consult both the class and its superclasses recursively.
You can access the constructors of a class like this:
Constructor constructors = aClass.getConstructors();
Constructors are covered in more detail in the text on Constructors.
You can access the methods of a class like this:
Method method = aClass.getMethods();
Methods are covered in more detail in the text on Methods.
You can access the fields (member variables) of a class like this:
Field method = aClass.getFields();
Fields are covered in more detail in the text on Fields.
You can access the class annotations of a class like this:
Annotation annotations = aClass.getAnnotations();
Annotations are covered in more detail in the text on Annotations.