Member-only story
Java Reflection API is an advanced feature of Java used for introspecting and modifying applications from class levels during runtime. As it is powerful, Reflection API should be used by developers who understand the fundamental & philosophy of Java language. Reflection makes the impossible to possible during runtime. All class members can be retrieved by using Reflection API and then can be displayed. Even, private methods can be invoked.

This functionality is very powerful yet it should be used very careful while building our software. There are three main points that we need to take care of while using Reflection API;
- Performance: Reflection API has slower performance as it is dynamically resolved.
- Exposure of Internals: Private methods or fields can be reached by using Reflection API so can cause unexpected impacts. It also breaks encapsulation & abstraction of internals.
- Security: It requires a runtime permission to introspect & modify the application.
And main features of Reflection API are;
- Extensibility Feature: An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.
- Debugger & Test Tools: Debuggers even need private members of a class or test need calling of methods exposed by the class in order to understand the level of coverage by using Reflection API.
- Visual Development Environments: IDEs can use type information or class members by using Reflection.
You can also refer to Reflection API Tutorial in order to reach these info related to Reflection API.
How to Get Instance of Classes
There are three main ways to get instance of classes in Reflection.
- forName() method → It loads the class dynamically and returns the instance of a Class. It should be used if you know the exact name of the class.
- getClass() method → It returns the instance of the class and should be used if type of the Class is known.
- .class syntax → If there is no instance of the class, .class syntax can be used to get the instance of the class.