Private method invoked Getting Interfaces The name of the constructor is: ReflectionDemo Method methodCall3 = cls.getDeclaredMethod("method3") Method methodCall2 = cls.getDeclaredMethod("method1") Method methodCall1 = cls.getDeclaredMethod("method2", int.class) įield field = cls.getDeclaredField("str") ("\nThe public methods of the class are: ") ("\nThe name of the constructor is: " + constructor.getName()) ("\nThe name of the class is: " + cls.getName()) Ĭonstructor constructor = cls.getConstructor() ReflectionDemo obj = new ReflectionDemo() Methods: The getMethods() method gives all the public methods of the class to which an object belongs. Constructors: The getConstructors() method returns all the public constructors of the class to which an object belongs.ģ. Class: The getClass() method gives the name of the class to which an object belongs.Ģ. We can use Reflection to get the information about:ġ. True Getting Information using Reflection API Let’s see the example of forName() method to get the instance of the class: class Demo We cannot use this name for primitive types.
We should use it only if we know the fully qualified name of the class. This method returns the instance of Class class. The forName() method loads the class dynamically or at runtime. We will discuss each of the methods with an example: 1) The forName() method of Class class There are three ways to get the object of Class class. How can we get the object of Class class? It returns the total number of constructors of this class. Public Constructor getDeclaredConstructors() This method returns the method class instance. Public Method getDeclaredMethod(String name,Class parameterTypes) It returns the total number of methods of the class. It returns the total number of fields in the class. It returns the superclass or parent class reference. This method checks if it is an interface. This method loads the class and returns the reference of Class. Public static Class forName(String className) It provides methods to examine and change the behavior of the class at runtime.Ĭommonly used methods of the Class class: Method.It provides methods to get the metadata of a class at runtime.Integrated Development Environment(IDEs) such as Eclipse, MyEclipse, NetBeans, etc.The Reflection API of Java is mainly used in: Through reflection, we can call a method at runtime independent of their access specifier.
Reflection gives us data about the class with the associated objects and the methods for that class.The classes required for reflection in Java are present in the package.Some points about Reflection in Java are: There are two packages- java.lang and that provide classes for Java Reflection. The is a class that provides many methods that we can use to get metadata of the class and to examine and change the runtime behavior of a class.
It is a process of examining or modifying the run time behavior of a class at run time. Reflection in Java is an API(Application Programming Interface) that is used at runtime to analyze or change classes, methods, and interfaces. The method is: private native void ( you updated with latest technology trends, Join TechVidvan on Telegram What is Reflection in Java? The method is: private native void 0(int) The method is: public void ($UncaughtExceptionHandler) The method is: public $UncaughtExceptionHandler () The method is: public static $UncaughtExceptionHandler () The method is: public static void ($UncaughtExceptionHandler) The method is: public final synchronized void () The method is: public final native boolean () The method is: public static void (long,int) throws The method is: public static native void (long) throws The method is: private static synchronized long () The method is: private static synchronized int () The method is: public final void (boolean) The method is: public static native void () The method is: public static native boolean () The method is: private void (,long,boolean) The method is: public final void () throws The method is: public final synchronized void (long) throws The method is: public final synchronized void (long,int) throws The method is: public synchronized void () The method is: private static native void () The method is: private native boolean (boolean) Method methods = c.getDeclaredMethods() įor(int i = 0 i < methods.length i++) Output The method is: public void ()
However, the inherited methods are not included.Īlso, the getDeclaredMethods() method returns a zero length array if the class or interface has no methods or if a primitive type, array class or void is represented in the Class object.Ī program that demonstrates this is given as follows − Example This method returns an array that contains all the Method objects with public, private, protected and default access. The methods of a class can be listed using the () method.