Java — Reflection in Java | Code Factory

package com.example.java.programming;/* A simple Java program to demonstrate the use of reflection */
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* @author code.factory
*/
/* class whose object is to be created */
class Demo {

/* creating a private field */
private String s;

/* creating a public constructor */
public Demo() {
s = "Code Factory";
}

/* creating a public method with no arguments */
public void method1() {
System.out.println("The string is: " + s);
}

/* creating a public method with int as argument */
public void method2(int n) {
System.out.println("The number is: " + n);
}

/* creating a private method */
private void method3() {
System.out.println("Private method invoked");
}
}
public class Test {
public static void main(String... strings) throws Exception {

/* creating object whose property is to be checked */
Demo demo = new Demo();

/* creating class object from the object using getClass() method */
Class cls = demo.getClass();
System.out.println("The name of class is: " + cls.getName());

/* getting the constructor of the class through the object of the class */
Constructor constructor = cls.getConstructor();
System.out.println("The name of constructor is: " + constructor.getName());

System.out.println("The public methods of class are:");

/* getting methods of the class through the object
* of the class by using getMethods() */
Method[] methods = cls.getMethods();

for(Method method : methods) {
System.out.println(" - " + method.getName());
}

/* creates object of desired method by providing the
* method name and parameter class as arguments to
* the getDeclaredMethod() */
Method methodCall1 = cls.getDeclaredMethod("method2", int.class);

/* invokes the method at runtime */
methodCall1.invoke(demo, 34);

/* creates object of the desired field by providing
* the name of field as argument to the
* getDeclaredField() method */
Field field = cls.getDeclaredField("s");

/* allows the object to access the field irrespective
* of the access specifier used with the field */
field.setAccessible(true);

/* takes object and the new value to be assigned
* to the field as arguments */
field.set(demo, "CODE");

/* Creates object of desired method by providing the
* method name as argument to the getDeclaredMethod() */
Method methodCall2 = cls.getDeclaredMethod("method1");

/* invokes the method at runtime */
methodCall2.invoke(demo);

/* Creates object of the desired method by providing
* the name of method as argument to the
* getDeclaredMethod() method */
Method methodCall3 = cls.getDeclaredMethod("method3");

/* allows the object to access the method irrespective
* of the access specifier used with the method */
methodCall3.setAccessible(true);

/* invokes the method at runtime */
methodCall3.invoke(demo);
}
}
The name of class is: com.example.java.programming.Demo
The name of constructor is: com.example.java.programming.Demo
The public methods of class are:
- method2
- method1
- wait
- wait
- wait
- equals
- toString
- hashCode
- getClass
- notify
- notifyAll
The number is: 34
The string is: CODE
Private method invoked

Uses of Reflection

Drawbacks of Reflection

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store