Java Reflection is an API provided in Java programming language that makes it possible to inspect classes, methods, fields etc. at runtime; without knowing their names at compile time. In addition to inspecting classes and its members, it is also possible to instantiate objects, call methods and set field values using reflection.
Java Reflection API is commonly used in the development of developer tools.
Visual Development Environments:Visual development environments use Java reflection to make the development process easier and more efficient by prompting the correct types and values to the developer
Class Browsers:Class browsers inspect class and its members
Debuggers and Testing Tools:
Performance overhead: Reflection works by dynamically resolving and inspecting classes and its members. with this flexibility comes its disadvantage - certain java virtual machine optimizations cannot be performed when types are resolved dynamically leading to slower performance as compared to normal class and method operations. When an operation can be performed non-reflective as well as reflective operation, always prefer the non-reflective operation. In performance sensitive applications, reflective operations must be avoided in loops and frequently called sections of code.
Security Restrictions: There are certain security impacts to using Reflection. Reflection needs a runtime permission which may not be available when running under a security manager, such as in an Applet.
Exposure of Internals: Java reflection enables us to perform certain operations which are illegal in non-reflective operations. For example - We can access the private members of a class which is illegal with non-reflective operations. This leads to dysfunctional and unportable code, and breaks the object oriented principle of abstraction and containment.
Every type; including reference types, primitive types (int, char etc.) and arrays have an associated java.lang.Class object. To perform reflection operation on a class, we have to first get its associated class object. Following are the different ways to get a Class object, depending on what the code has access to - object, type, class or name of class.
Class.forName(): If the code has access to a fully-qualified class name you can use 'Class.forName()' to get the class object of the fully-qualified class name.
Object.getClass(): If the code has access to an instance object you can use 'Object.getClass()' syntax to get the class object for the object instance.
Type.class:If the code has access to the type of class, you can use 'Type.class' syntax to get the class object for the type.
The package of a class can be accessed by calling the method getPackage() on the class object.
Class myClass = Class.forName('java.lang.String');
Package package = myClass.getPackage();
The interfaces of a class can be accessed by calling the method getInterfaces() on the class object...
*** See complete answer and code snippet in the Java Interview Guide.
The parent or super class of a class can be accessed by calling the method getSuperClass() on the class object...
*** See complete answer and code snippet in the Java Interview Guide.
Class access modifiers are the access modifiers such as public, private etc. that a class is declared with. Class modifiers can be accessed calling the method getModifiers() on the class object...
*** See complete answer and code snippet in the Java Interview Guide.
Constructors of a class can be accessed by calling the method getConstructors() on the class object...
*** See complete answer and code snippet in the Java Interview Guide.
Fields of a class can be accessed by calling the method getFields() on the class object...
*** See complete answer and code snippet in the Java Interview Guide.
Annotations of a class can be accessed by calling the method getAnnotations() on the class object...
*** See complete answer and code snippet in the Java Interview Guide.