Java Reflection API: All You Need To Know
Java Reflection is a process of examining or modifying the run time behavior of a class at run time. Java Reflection API is used to manipulate class and its members which include fields, methods, constructor, etc. at runtime. In This article, we would understand Java Reflection API in detail.
This article will focus on the following pointers:
- Where Java Reflection API used?
- Class in java.lang.reflect Package
- Methods used in java. lang.Class
- How to get the object of Class class?
- Advantages and Disadvantages of Using Java Reflection API
So let us get started with these pointers in this article on Java Reflection API
Where is Java Reflection API used?
The Reflection API is mainly used in:
- IDE (Integrated Development Environment) e.g. Eclipse, MyEclipse, NetBeans etc.
- Debugger
- Test Tools etc.
So what is Class in Java lang reflect package?
Class in java.lang.reflect Package?
Following is a list of various Java classes in java.lang.package to implement reflection-
- Field: This class is used to gather declarative information such as datatype, access modifier, name and value of a variable.
- Method: This class is used to gather declarative information such as access modifier, return type, name, parameter types and exception type of a method.
- Constructor: This class is used to gather declarative information such as access modifier, name and parameter types of a constructor.
- Modifier: This class is used to gather information about a particular access modifier.
No let us take a look at Java Reflection API methods,
Methods used in java.lang.Class
Let us move forward with article,
How to get the object of Class class?
There are 3 ways to get the instance of Class class. They are as follows:
- forName() method of Class class
- getClass() method of Object class
- the .class syntax
forName() method of Class class
- is used to load the class dynamically.
- returns the instance of Class class.
- It should be used if you know the fully qualified name of class.This cannot be used for primitive types.
Let’s see the simple example of forName() method.
class Simple{}
class Test{
public static void main(String args[]){
Class c=Class.forName("Simple");
System.out.println(c.getName());
}
}
Output:
Simple
Java Reflection: API getClass() method of Object class
It returns the instance of Class class. It should be used if you know the type. Moreover, it can be used with primitives.
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
The .class syntax
If 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 type also.
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
Now let us continue with this Java Reflection API article
Advantages and Disadvantages of Using Java Reflection API
Advantages of using Java Reflection API
- 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 in classes.
Disadvantages of using Java Reflection API
- Performance Overhead: Reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.
- Exposure of Internals: Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.
Thus we have come to an end of this article on ‘Java Reflection API’. If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.
Do look out for other articles in this series which will explain the various other aspects of Java.
1. Object Oriented Programming
5. Java String
6. Java Array
8. Java Threads
9. Introduction to Java Servlets
11. Exception Handling in Java
14. Java Programs
15. Kotlin vs Java
16. Dependency Injection Using Spring Boot
19. Java Tutorial
22. Socket Programming In Java
25. Library Management System Project in Java
26. Trees in Java
28. Top Data Structures & Algorithms in Java
30. Top 55 Servlet Interview Questions
34. Java Collections Interview Questions and Answers
35. How to Handle Deadlock in Java?
36. Top 50 Java Collections Interview Questions You Need to Know
37. What is the concept of String Pool in Java?
38. What is the difference between C, C++, and Java?
39. Palindrome in Java- How to check a number or string?
40. Top MVC Interview Questions and Answers You Need to Know
41. Top 10 Applications of Java Programming Language
42. Deadlock in Java
43. Square and Square Root in Java
45. Operators in Java and its Types
49. Hibernate Interview Questions And Answers
42. Deadlock in Java
43. Square and Square Root in Java
Originally published at https://www.edureka.co on July 18, 2019.