Java Reflection - Member


Any class has member fields and member functions or methods. These are defined by the interface Member. Using the Java Reflection API, you can dynamically add Members to a given object. Such Members of the class are called Synthetic members. Members of a class have Modifiers and Annotations. The Member interface defines basic methods for interacting with a member of the class. It has 4 major implementations of this interface - Field, Executable, Method and Constructor


The fields in a class are represented by the class Field. You can identify the fields in a given class by using the method getFields().
Field[] fields = c.getFields()
You can also get a particular field by using its name
Field field = c.getField("fieldName")
Just like getField() and getFields(), you have two other methods getDeclaredField() and getDeclaredFields(). These refer fields that are declared within the given class and not inherited. The syntax is similar.
Field[] fields = c.getDeclaredFields()
Field field = c.getDeclaredField("fieldName")
For a given Field object, you can identify the class where it is declared, by using the getDeclaringClass() method.
Field f = c.getDeclaredField("fileName");
Class c = f.getDeclaringClass();


An Executable is a Member of the Class that can be executed. It could be a Method or a Constructor.


This defines any method in the class. You can get the list of methods in a class using getMethods() and getDeclaredMethods(). These are similar to the getFields() and getDeclaredFields(). You can also access individual methods by name and parameters. The Reflection API also allows you the invoke a given method on an object


Constructor is another type of Executable. It has most features similar to the Method. But instead of invoking the constructor (like the methods), you can trigger the constructor to get a newInstance()

Important Methods

Some of the common methods on the above classes are listed here.
MembergetDeclaringClass()Returns the Class object representing the class or interface that declares the member or constructor represented by this Member.
MembergetModifiers()Returns the Java language modifiers for the member or constructor represented by this Member, as an integer.
MembergetName()Returns the simple name of the underlying member or constructor represented by this Member.
MemberisSynthetic()Returns true if this member was introduced by the compiler; returns false otherwise.
FieldgetDeclaringClass()Returns the Class in which the Field was declared.
MethodgetDeclaringClass()Returns the Class in which the method was declared.
Methodinvoke(Object obj, Object... args)Invokes the underlying method represented by this Method object, on the specified object with the specified parameters.
ConstructorgetDeclaringClass()Returns the Class in which the constructor was declared.
ConstructornewInstance(Object... initargs)Uses the constructor represented by this Constructor object to create and initialize a new instance of the constructor's declaring class, with the specified initialization parameters.