Powered by Blogger.

What is Reflection and How it Works in Java

>> Monday, September 23, 2013

Reflection is the process of obtaining runtime information about the class or interface.”
Reflection in Java
Runtime information is nothing but deal with the following:
  1. Finding the name of the class or interface. 
  2.  Finding the data members of the class or interface. 
  3. Finding number of constructors (default constructor and number of parameterized constructors).
  4. Number of instance methods.
  5. Number of static methods. 
  6. Determining modifiers of the class (modifiers of the class can be public, final, public + final,abstract and public + abstract).
  7. Obtaining super class of a derived class. 
  8. Obtaining the interfaces which are implemented by various classes.
Real time applications of reflection:
  1. Development of language compiler, debuggers, editors and browsers. 
  2. In order to deal with reflection in java, we must import a predefined package called java.lang.reflect.*
  3. 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:
  1. 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)

    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:
What is Reflection in java_javabynataraj_001
      3. When an object is given at runtime, we must find runtime information about current class and its super class.
What is Reflection in java_javabynataraj_002
#1. Write a java program to print name of the current class and its super class name.
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 ());
What is Reflection in java_javabynataraj_003
      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:
What is Reflection in java_javabynataraj_004
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:
 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).
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
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]);
   } 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
What is Reflection in java_javabynataraj_005

#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]);
   } catch (ClassNotFoundException cnfe) {
    System.out.println(args[0] + " DOES NOT EXISTS...");

 static void printHierarchy(Class<?> c) {
  Class<?> c1 = c;
  String cname = c1.getName();
  Class<?> sc = c1.getSuperclass();
  while (sc != null) {
   cname = sc.getName();
   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.
What is Reflection in java_javabynataraj_006

Reference Books:

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2014. All rights reserved.