Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: All
Sep 8, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

What Reflection Means in Java

Through reflection, Java code can obtain information about, and perform operations on, data fields, methods, and constructors of loaded classes. Of coure, there are security restrictions.

The reflection API (encapsulated in "java.lang.reflect" package) represents classes, interfaces, and objects in the current Java Virtual Machine. Using the reflection API in tandem with "java.lang.Class" will render power to a wide range of computational initiatives, such as, GUI building, or debugger manufacturing.

Through reflection you can:
1. Determine the class of an object.
2. Get information about a class's modifiers, fields, methods, constructors, and superclasses.
3. Find out what constants and method declarations belong to an interface.
4. Create an instance of a class whose name is not known until runtime.
5. Get and set the value of an object's field, even if the field name is unknown to your program until runtime.
6. Invoke a method on an object, even if the method is not known until runtime.
7. Create a new array, whose size and component type are not known until runtime, and then modify the array's components.

To work with the API, you need to obtain an instance of the Class class of an object, as in:

	java.lang.Class c = Class.forName("java.util.GregorianCalendar");

Note that fully qualified names are essential. Then, you can obtain the class modifiers through a call like:

	int modifier = c.getModifiers();
Then, you can test the modifiers using "public static boolean isPublic(int mod)", "public static boolean isAbstract(int mod)", and "public static boolean isFinal(int mod)" of java.lang.reflect.Modifier class. You can use a number of useful methods in java.lang.Class to know the super class of a class through "public native Class getSuperclass()", the interfaces implemented by a class through "public native Class[] getInterfaces()", constructors of the class through "public Constructor[] getConstructors()throws SecurityException", the field names of a class through "public Field[] getFields() throws SecurityException", the methods of a class through "public Method[] getMethods() throws SecurityException".

Then you can dynamically create an instance of the class through "public native Object newInstance() throws InstantiationException, IllegalAccessException", as in:

                Object obj = c.newInstance();
You can also use some methods of java.lang.reflect.Field to check the field values of the instance object you just created, as in:
 	    c = obj.getClass();
                Field[] publicFields = c.getFields();
                for (int i = 0; i < publicFields.length; i++) 
                    String fieldName  = publicFields[i].getName();
                   Class fieldType = publicFields[i].getType();
                   String fieldTypeName = fieldType.getName();
                   Object fieldValue = publicFields[i].get(obj);
	catch(Exception e)
		//handle exception
Behrouz Fallahi
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date