0% found this document useful (0 votes)
2 views

Java Reflection

Java Reflection is a feature that allows programs to inspect and manipulate classes, methods, fields, and constructors at runtime. It provides capabilities to retrieve class information, method details, field data, and constructor specifics using the Class class and the java.lang.reflect package. Examples demonstrate how to use reflection to access class names, modifiers, and method information in Java.

Uploaded by

sweta.23bce11754
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Reflection

Java Reflection is a feature that allows programs to inspect and manipulate classes, methods, fields, and constructors at runtime. It provides capabilities to retrieve class information, method details, field data, and constructor specifics using the Class class and the java.lang.reflect package. Examples demonstrate how to use reflection to access class names, modifiers, and method information in Java.

Uploaded by

sweta.23bce11754
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Java Reflection

Reflection is a powerful feature that allows a program


to examining or retrieving information about classes,
methods(member function), fields(Data members),
constructors, and other components of a program at
runtime.

What Can you examining or retrieving Using


Reflection?
1. Class Information
a. Get the name of the class.
b. Check its package and superclass.
c. Identify its implemented interfaces.
2. Methods(Member Function)
a. List all methods of a class.
b. Get method names, return types, and
parameters.
c. Check method modifiers (public, private,
static, etc.).
3. Data Members (Variables)
a. List all fields in a class.
b. Get their data types and values.
c. Check their access modifiers.
4. Constructors
a. Get details about available constructors.
b. Retrieve parameter types
How Java Reflection Implemented:
In Java, there is a special Class named Class that
stores all the information about objects and classes at
runtime.

This Class object helps us inspect and manipulate


classes, objects, methods, and fields while the program
is running.

How to Get a Class Object?


There are 3 ways to get a Class object:
a. Using .getClass() on an object
b.Using Class.forName("className")
c. Using ClassName.class

Why is Class Useful?


With a Class object, we can:
a. Get the class name
b.Get methods and fields
c. Call private methods (if needed)
Example: Java Class Reflection

import java.lang.Class;
import java.lang.reflect.*;

class Animal {
}

// put this class in different Dog.java file


public class Dog extends Animal {
public void display() {
System.out.println("I am a dog.");
}
}
// put this in Main.java file
class Main {
public static void main(String[] args) {
try {
// create an object of Dog
Dog d1 = new Dog();

// create an object of Class


// using getClass()
Class obj = d1.getClass();

// get name of the class


String name = obj.getName();
System.out.println("Name: " + name);

// get the access modifier of the class


int modifier = obj.getModifiers();
// convert the access modifier to string
String mod = Modifier.toString(modifier);
System.out.println("Modifier: " + mod);

// get the superclass of Dog


Class superClass = obj.getSuperclass();
System.out.println("Superclass: " +
superClass.getName());
}

catch (Exception e) {
e.printStackTrace();
}
}
}
Output
Name: Dog
Modifier: public
Superclass: Animal

In the above example, we have created a superclass:


Animal and a subclass: Dog.

Now, we are trying to inspect the class Dog.

Class obj = d1.getClass();

Here, we are creating an object obj of Class using the


getClass() method. Using the object, we are calling
different methods of Class.

obj.getName() - returns the name of the class


obj.getModifiers() - returns the access modifier of the
class
obj.getSuperclass() - returns the super class of the
class

Reflecting Fields, Methods, and Constructors


The package java.lang.reflect provides classes that can
be used for manipulating class members.

For example,
Method class - provides information about methods in
a class
Field(Data members) class - provides information
about fields in a class
Constructor class - provides information about
constructors in a class.
Reflection of Java Methods
The Method class provides various methods that can
be used to get information about the methods present
in a class.
For example,
import java.lang.Class;
import java.lang.reflect.*;

class Dog {

// methods of the class


public void display() {
System.out.println("I am a dog.");
}

private void makeSound() {


System.out.println("Bark Bark");
}
}

class Main {
public static void main(String[] args) {
try {
// create an object of Dog
Dog d1 = new Dog();

// create an object of Class


// using getClass()
Class obj = d1.getClass();

// using object of Class to


// get all the declared methods of Dog
Method[] methods = obj.getDeclaredMethods();

// create an object of the Method class


for (Method m : methods) {

// get names of methods


System.out.println("Method Name: " +
m.getName());
// get the access modifier of methods
int modifier = m.getModifiers();
System.out.println("Modifier: " +
Modifier.toString(modifier));

// get the return types of method


System.out.println("Return Types: " +
m.getReturnType());
System.out.println(" ");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Output
Method Name: display
Modifier: public
Return Types: void
Method Name: makeSound
Modifier: private
Return Types: void

In the above example, we are trying to get information


about the methods present in the Dog class. As
mentioned earlier, we have first created an object obj
of Class using the getClass() method.
Method[] methods = obj.getDeclaredMethod();

Here, the getDeclaredMethod() returns all the


methods present inside the class.

Also, we have created an object m of the Method class.


Here,

m.getName() - returns the name of a method


m.getModifiers() - returns the access modifier of
methods in integer form
m.getReturnType() - returns the return type of
methods
The Method class also provides various other methods
that can be used to inspect methods at run time.
Reference:
https://www.programiz.com/java-programming/
reflection

You might also like