What is Reflection and How it Works in Java
“Reflection is the process of obtaining runtime information about the class or interface.”
4. We know the class name at runtime and we have to obtain the runtime information about the class.
To perform the above we must use the method java.lang.Class and whose prototype is given below:
When we use the forName as a part of java program it performs the following operations:
For example: java.lang.Class is called Singleton class
#2. Program to find name of the class and its super class name by passing the class name at runtime.
Here pass any fully qualified class name at runtime as arguments
#3. Program to print super class hierarchy at a current class which is passed from command prompt
Output:
Reference Books:
Runtime information is nothing but deal with the following:
- Finding the name of the class or interface.
- Finding the data members of the class or interface.
- Finding number of constructors (default constructor and number of parameterized constructors).
- Number of instance methods.
- Number of static methods.
- Determining modifiers of the class (modifiers of the class can be public, final, public + final,abstract and public + abstract).
- Obtaining super class of a derived class.
- Obtaining the interfaces which are implemented by various classes.
Real time applications of reflection:
- Development of language compiler, debuggers, editors and browsers.
- In order to deal with reflection in java, we must import a predefined package called java.lang.reflect.*
- The package reflect contains set of predefined classes and interfaces which are used by the programmer to develop reflection applications.
Number of ways to obtain run-time information about a class (or) number of ways to get an object
of a class called Class:
The predefined class called Class is present in a package called java.lang.Class (fully qualified name of a class called Class is java.lang.Class).
In java we have 4 ways to deal with or to create an object of java.lang.Class, they are:
- When we know that class name at compile time and to get runtime information about the class, we must use the following:
Ex1: Class cls = cs.class (cs - user defined class)
Ex2: Class cc = java.lang.String.class (String - predefined class)
Ex2: Class cc = java.lang.String.class (String - predefined class)
2. When we know the object name at runtime, to get the class name or class type of the runtime object, we must use the following:
Ex1: cs obj = new cs(); Class cls = obj.getClass(); Ex2: String str = new String("Hello World"); Class cls = str.getClass();
getClass is the predefined method present in a predefined class called java.lang.Object and whose prototype is given below:
3. When an object is given at runtime, we must find runtime information about current class and its super class.
package com.javabynataraj; class Reflect { public static void main (String [] args){ String s=new String ("HELLO World"); printSuperclass (s); } static void printSuperclass (Object s){ Class cc=s.getClass (); Class sc=cc.getSuperclass (); System.out.println ("NAME OF CURRENT CLASS : "+cc.getName ()); System.out.println ("NAME OF THE SUPER CLASS : "+sc.getName ()); } };Output:
4. We know the class name at runtime and we have to obtain the runtime information about the class.
To perform the above we must use the method java.lang.Class and whose prototype is given below:
When we use the forName as a part of java program it performs the following operations:
- It can create an object of the class which we pass at runtime.
- It returns runtime information about the object which is created.
For Example:
try{ Class c=Class.forName (“java.awt.Button”); }catch (ClassNotFoundException cnfe){ System.out.println (“CLASS DOES NOT EXIST...”); }
forName is taking String as an argument. If the class is not found forName method throws an exception called ClassNotFoundException.
Here, forName method is a factory method (a factory method is one which return type is similar to name of the class where it presents).
Every factory method must be static and public. The class which contains a factory method is known as Singleton class (a java class is said to be Singleton class through which we can create single object per JVM).
#2. Program to find name of the class and its super class name by passing the class name at runtime.
Here pass any fully qualified class name at runtime as arguments
package com.javabynataraj; class TestReflect { public static void main(String[] args) { if (args.length == 0) { System.out.println("PLEASE PASS THE CLASS NAME..!"); } else { try { Class<?> c = Class.forName(args[0]); printSuperclass(c); } catch (ClassNotFoundException cnfe) { System.out.println(args[0] + " DOES NOT EXISTS..."); } }// else }// end main static void printSuperclass(Class<?> c) { String s = c.getName(); Class<?> sc = c.getSuperclass(); String sn = sc.getName(); System.out.println(sn + " IS THE SUPER CLASS OF " + s); }// printSuperclass }Output:
#3. Program to print super class hierarchy at a current class which is passed from command prompt
package com.javabynataraj; class Hierarchy { public static void main(String[] args) { if (args.length == 0) { System.out.println("PLEASE PASS THE CLASS NAME..!"); } else { try { Class<?> c = Class.forName(args[0]); printHierarchy(c); } catch (ClassNotFoundException cnfe) { System.out.println(args[0] + " DOES NOT EXISTS..."); } } } static void printHierarchy(Class<?> c) { Class<?> c1 = c; String cname = c1.getName(); System.out.println(cname); Class<?> sc = c1.getSuperclass(); while (sc != null) { cname = sc.getName(); System.out.println(cname); c1 = sc; sc = c1.getSuperclass(); } } }While running the program pass the any class name as argument like : java.util.ArrayList. Here we can see the Hierarchy of ArrayList classes.
Output:
Reference Books: