java.lang.reflect package encapsulates several important interfaces and classes. These classes and interface define methods which are used for reflection. Basically these class are used for finding and representing information of class like method,field etc.

  • Some Important Classes of java.lang.reflect package

    Commonly used class are

    Class What it does ?
    Field provide information about field
    Method provide information about method
    Constructor gives information about constructor of a class
    Array allow you to dynamically create and manipulate arrays
    Modifier provide information about class and member access modifier
    Proxy supports dynamic proxy classes
  • Getting the Fields of any class at runtime using reflection

    getFields() and getDeclaredFields() methods of Class class is used to find the fields of any class.

    • getFields() -this method returns array of Field containing all the public member of the class represented by this Class object.

    • getDeclaredFields() -this method returns array of Field containing all the members including public of the class represented by this Class object.


    Example

    import java.lang.reflect.*;
    
    class Test {
    	public int i;
    	public static int j;
    	int myvariable;
    	Test() {
    		System.out.println("Default Constructor...");
    	}
    
    	Test(int a) {
    		System.out.println("Parameterized Constructor int type argu.");
    	}
    }
    
    class Executor {
    	public static void main(String[] args) throws Exception {
    
    		Class c = Class.forName("Test");
    
    		// getting all the public field at run time
    		Field[] f = c.getFields();
    		for (Field i : f) {
    			System.out.println(i.getName());
    		}
    		System.out.println("#########################");
    		// getting all the public and non public field
    		Field[] ff = c.getDeclaredFields();
    		for (Field i : ff) {
    			System.out.println(i.getName());
    		}
    	}
    }

    Output:
    i
    j
    #########################
    i
    j
    myvariable
  • Getting the Constructors of any class at runtime using reflection

    getConstructors() and getDeclaredConstrucotrs() methods of Class class is used to find the constructors of any class at run time.

    • getConstructors() -this method returns array of constructor containing all the public constructor of the class represented by this Class object.

    • getDeclaredConstructors() -this method returns array of constructor containing all the constructors (including public ) of the class represented by this Class object.


    Example

    
    import java.lang.reflect.Constructor;
    
    class Test {
    
        private int a;
        private String name;
    
        public Test(int a, String name) {
            this.a = a;
            this.name = name;
        }
    
        Test() {
        }
    
        public String getName() {
            return this.getName();
        }
    
        public int getA() {
            return this.a;
        }
    }
    
    class OnlyJavaTech {
    
        public static void main(String[] args) throws ClassNotFoundException {
            Class cc = Class.forName("Test");
    
            //getting the public constructor.....
            Constructor[] t = cc.getConstructors();
            for (Constructor x : t) {
                System.out.println(x);
            }
    
            System.out.println("########################################");
            //getting all constructor including public
            Constructor[] tt = cc.getDeclaredConstructors();
            for (Constructor x : tt) {
                System.out.println(x);
            }
    
        }
    
    }
    

    Output:
    public Test(int,java.lang.String)
    ########################################
    public Test(int,java.lang.String)
    Test()
    

What next?

The next topic is javap tool in java

Share this page