@KingBomm Tech #java #ReflectionApi #KingBommTech
⚡⚡Reflection API:🔥🔥
Reflection is an API which is used to examine or modify the behavior of methods, classes, interfaces at runtime.
⭐⭐The required classes for reflection are provided under java.lang.reflect package.
⭐⭐Reflection gives us information about the class to which an object belongs and also the methods of that class which can be executed by using the object.
⭐⭐Through reflection we can invoke methods at runtime irrespective of the access specifier used with them.
Example :
public class Test {
// creating a private field
private String s;
// creating a public constructor
public Test() { s = "KingBomm Tech"; }
// Creating a public method with no arguments
public void m1() {
System.out.println("The string is " + s);
}
// Creating a public method with int as argument
public void m2(int n) {
System.out.println("The number is " + n);
}
// creating a private method
private void m3() {
System.out.println("Private method invoked");
}
}
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Demo {
public static void main(String[] args) {
// Creating object whose property is to be checked
Test obj = new Test();
// Creating class object from the object using getclass method
Class cls = obj.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;
try {
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();
// Printing method names
for (Method method:methods)
System.out.println(method.getName());
// Creates object of desired method by providing the method name as argument to the getDeclaredMethod
Method m1 = cls.getDeclaredMethod("m1");
// invokes the method at runtime
m1.invoke(obj);
// creates object of desired method by providing the
// method name and parameter class as arguments to
// the getDeclaredMethod
Method m2 = cls.getDeclaredMethod("m2",int.class);
// invokes the method at runtime
m2.invoke(obj, 19);
// 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(obj, "JAVA");
// invokes the method at runtime
m1.invoke(obj);
// Creates object of the desired method by providing the name of method as argument to the getDeclaredMethod method
Method m3 = cls.getDeclaredMethod("m3");
// allows the object to access the method irrespective
// of the access specifier used with the method
m3.setAccessible(true);
// invokes the method at runtime
m3.invoke(obj);
} catch (NoSuchMethodException |
SecurityException |
IllegalAccessException |
IllegalArgumentException |
InvocationTargetException |
NoSuchFieldException e) {
e.printStackTrace();
}
}
}
⚡⚡Advantages 🔥🔥
⭐⭐Extensibility Features
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.
⭐⭐Debugging and testing tools
Debuggers use the property of reflection to examine private members on classes.
⚡⚡Drawbacks 🔥🔥
⭐⭐Performance Overhead
Reflective operations have slower performance than their non-reflective counterparts.
⭐⭐Exposure of Internals
Reflective code breaks abstractions.
🌟🌟Share, Support, Subscribe!!!🔥🔥
Another Application with source Code:
⭐⭐ Food Delivery Application Project 🔥 :
• Online Food Delivery Application project u...
⭐⭐ Customer Service Disk Project 🔥:
• Customer Service Desk project in #java #se...
⭐⭐ Online Quiz Application Project 🔥 :
• Online Quiz Application project using #Jav...
⭐⭐ Ngo Font-End Web Designing Project 🔥 :
• Making NGO Front-End web designing using #...
⭐⭐ Online Banking Application Project 🔥 :
• Banking application using java | Bank Mana...
⭐⭐ Online Pharmacy Application Project 🔥 :
• Online Pharmacy Application project using ...
👉Angular JS Complete Course total Free 🔥 :
• Angular JS 🔥
Follow Me On Social Media
Twitter► / kingbommtech
Instagram► / kingbomm54
Email ► kinshukmaity555@gmail.com
Share, Support, Subscribe!!!
On this page of the site you can watch the video online Reflection api in java to access private methods with Example | interview questions for experienced with a duration of hours minute second in good quality, which was uploaded by the user Java2web 09 March 2022, share the link with friends and acquaintances, this video has already been watched 149 times on youtube and it was liked by 3 viewers. Enjoy your viewing!