Reflection is an API that is used to examine or modify the behavior of methods, classes, and interfaces at runtime. The required classes for reflection are provided under java.lang.reflect package which is essential in order to understand reflection. So we are illustrating the package with visual aids to have a better understanding as follows:

- Reflection gives us information about the class to which an object belongs and also the methods of that class that can be executed by using the object.
- Through reflection, we can invoke methods at runtime irrespective of the access specifier used with them.

Reflection can be used to get information about class, constructors, and methods as depicted below in tabular format as shown:
Class | The getClass() method is used to get the name of the class to which an object belongs. |
---|
Constructors | The getConstructors() method is used to get the public constructors of the class to which an object belongs. |
---|
Methods | The getMethods() method is used to get the public methods of the class to which an object belongs. |
---|
We can invoke a method through reflection if we know its name and parameter types. We use two methods for this purpose as described below before moving ahead as follows:
- getDeclaredMethod()
- invoke()
Method 1: getDeclaredMethod(): It creates an object of the method to be invoked.
Syntax: The syntax for this method
Class.getDeclaredMethod(name, parametertype)
Parameters:
- Name of a method whose object is to be created
- An array of Class objects
Method 2: invoke(): It invokes a method of the class at runtime we use the following method.
Syntax:
Method.invoke(Object, parameter)
Tip: If the method of the class doesn’t accept any parameter then null is passed as an argument.
Note: Through reflection, we can access the private variables and methods of a class with the help of its class object and invoke the method by using the object as discussed above. We use below two methods for this purpose.
Method 3: Class.getDeclaredField(FieldName): Used to get the private field. Returns an object of type Field for the specified field name.
Method 4: Field.setAccessible(true): Allows to access the field irrespective of the access modifier used with the field.
Important observations Drawn From 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 of classes.
- Performance Overhead: Reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code that are called frequently in performance-sensitive applications.
- Exposure of Internals: Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.
Example
prpravate
Output:

Example with code:
package contains Employee class with constructors , setters and getters method
Java
package org.geeksforgeeks.write;
import java.io.*;
import java.util.*;
public class Emp{
private int eid;
private double esal;
private String ename;
enum Week{
SUN,TUE,WED;
}
@interface MyAnno{}
public int getEid(){
return eid;
}
public void setEid(int eid,int num, char ch){
this.eid = eid;
}
public double getEsal(){
return esal;
}
public void setEsal(double esal,float data, String name){
this.esal = esal;
}
public String getEname(){
return ename;
}
public void setEname(String ename){
this.ename = ename;
}
// constructor
public Emp(int eid, double esal, String ename){
super();
this.eid = eid;
this.esal = esal;
this.ename = ename;
}
Emp(){
}
class A{
}
class B{
}
}
//Main Method with implementation of constructor, methods and classes
Java
package org.geeksforgeeks.write;
import java.lang.reflect.Method;
import java.lang.reflect.Constructor;
import java.lang.reflect.Parameter;
public class GFG {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) throws ClassNotFoundException {
Class c = Class.forName("org.geeksforgeeks.write.Emp");
// Constructor array
Constructor[] constructors = c.getDeclaredConstructors();
for(Constructor constructor : constructors) {
System.out.println("Name of Constructor : "+constructor);
System.out.println("Count of constructor parameter : "+constructor.getParameterCount());
Parameter[] parameters = constructor.getParameters();
for(Parameter parameter : parameters) {
System.out.println("Constructor's parameter : "+parameter);
}
System.out.println();
}
System.out.println();
// Method Array
Method[] methods = c.getDeclaredMethods();
System.out.println("Length of method : "+methods.length);
for(Method method : methods){
System.out.println("Method name: \t"+method);
System.out.println("Method return type : \t"+method.getReturnType());
System.out.println("Method parameter count: \t"+method.getParameterCount());
System.out.println();
Parameter[] parameters = method.getParameters();
for(Parameter parameter : parameters) {
System.out.println("Method's Parameter : "+parameter);
}
System.out.println();
}
System.out.println();
// Annotations
Class[] classes = c.getDeclaredClasses();
for(Class class1 : classes) {
System.out.println("class: "+class1);
System.out.println("Name of class: "+class1.getName());
}
// Annotations
Annotation[] anno = c.getDeclaredAnnotations();
for(Annotation annotation : anno) {
System.out.println("Annotation: "+annotation);
}
}
}
Output:
Name of Constructor : public org.geeksforgeeks.write.Emp(int,double,java.lang.String)
Count of constructor parameter : 3
Constructor's parameter : int arg0
Constructor's parameter : double arg1
Constructor's parameter : java.lang.String arg2
Name of Constructor : org.geeksforgeeks.write.Emp()
Count of constructor parameter : 0
Length of method : 6
Method name: public int org.geeksforgeeks.write.Emp.getEid()
Method return type : int
Method parameter count: 0
Method name: public void org.geeksforgeeks.write.Emp.setEid(int,int,char)
Method return type : void
Method parameter count: 3
Method's Parameter : int arg0
Method's Parameter : int arg1
Method's Parameter : char arg2
Method name: public double org.geeksforgeeks.write.Emp.getEsal()
Method return type : double
Method parameter count: 0
Method name: public void org.geeksforgeeks.write.Emp.setEsal(double,float,java.lang.String)
Method return type : void
Method parameter count: 3
Method's Parameter : double arg0
Method's Parameter : float arg1
Method's Parameter : java.lang.String arg2
Method name: public java.lang.String org.geeksforgeeks.write.Emp.getEname()
Method return type : class java.lang.String
Method parameter count: 0
Method name: public void org.geeksforgeeks.write.Emp.setEname(java.lang.String)
Method return type : void
Method parameter count: 1
Method's Parameter : java.lang.String arg0
class: class org.geeksforgeeks.write.Emp$A
Name of class: org.geeksforgeeks.write.Emp$A
class: class org.geeksforgeeks.write.Emp$B
Name of class: org.geeksforgeeks.write.Emp$B
class: interface org.geeksforgeeks.write.Emp$MyAnno
Name of class: org.geeksforgeeks.write.Emp$MyAnno
class: class org.geeksforgeeks.write.Emp$Week
Name of class: org.geeksforgeeks.write.Emp$Week
Similar Reads
Java Tutorial
Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts
Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers
Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java
Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java
Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java
Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling
Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Interface
An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Java Programs - Java Programming Examples
In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read