C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Java Reflection APIJava Reflection is a process of examining or modifying the run time behavior of a class at run time. The java.lang.Class class provides many methods that can be used to get metadata, examine and change the run time behavior of a class. The java.lang and java.lang.reflect packages provide classes for java reflection. Where it is usedThe Reflection API is mainly used in:
java.lang.Class classThe java.lang.Class class performs mainly two tasks:
Commonly used methods of Class class:
How to get the object of Class class?There are 3 ways to get the instance of Class class. They are as follows:
1) forName() method of Class class
Let's see the simple example of forName() method. FileName: Test.java class Simple{} public class Test{ public static void main(String args[]) throws Exception { Class c=Class.forName("Simple"); System.out.println(c.getName()); } } Output: Simple 2) getClass() method of Object classIt returns the instance of Class class. It should be used if you know the type. Moreover, it can be used with primitives. FileName: Test.java class Simple{} class Test{ void printName(Object obj){ Class c=obj.getClass(); System.out.println(c.getName()); } public static void main(String args[]){ Simple s=new Simple(); Test t=new Test(); t.printName(s); } } Output: Simple 3) The .class syntaxIf a type is available, but there is no instance, then it is possible to obtain a Class by appending ".class" to the name of the type. It can be used for primitive data types also. FileName: Test.java class Test{ public static void main(String args[]){ Class c = boolean.class; System.out.println(c.getName()); Class c2 = Test.class; System.out.println(c2.getName()); } } Output: boolean Test Determining the class objectThe following methods of Class class are used to determine the class object: 1) public boolean isInterface(): determines if the specified Class object represents an interface type. 2) public boolean isArray(): determines if this Class object represents an array class. 3) public boolean isPrimitive(): determines if the specified Class object represents a primitive type. Let's see the simple example of reflection API to determine the object type. FileName: Test.java class Simple{} interface My{} class Test{ public static void main(String args[]){ try{ Class c=Class.forName("Simple"); System.out.println(c.isInterface()); Class c2=Class.forName("My"); System.out.println(c2.isInterface()); }catch(Exception e){System.out.println(e);} } } Output: false true Pros and Cons of ReflectionJava reflection should always be used with caution. While the reflection provides a lot of advantages, it has some disadvantages too. Let's discuss the advantages first. Pros: Inspection of interfaces, classes, methods, and fields during runtime is possible using reflection, even without using their names during the compile time. It is also possible to call methods, instantiate a clear or to set the value of fields using reflection. It helps in the creation of Visual Development Environments and class browsers which provides aid to the developers to write the correct code. Cons: Using reflection, one can break the principles of encapsulation. It is possible to access the private methods and fields of a class using reflection. Thus, reflection may leak important data to the outside world, which is dangerous. For example, if one access the private members of a class and sets null value to it, then the other user of the same class can get the NullReferenceException, and this behaviour is not expected. Another demerit is the overhead in performance. Since the types in reflection are resolved dynamically, JVM (Java Virtual Machine) optimization cannot take place. Therefore, the operations performed by reflections are usually slow. ConclusionBecause of the above-mentioned cons, it is generally advisable to avoid using reflection. It is an advanced feature that should only be used by programmers or developers who have a good knowledge of the basics of the language. Always remember! Whenever reflection is used, the security of the application is compromised. Next Topics of Reflection API TutorialnewInstance() methodUnderstanding javap tool creating javap tool creating appletviewer tool Call private method from another class
Next TopicNew Instance() Method
|