MainMenu

Home Java Overview Maven Tutorials

Monday 15 March 2021

Reflection In Java



Reflection in java





Java Reflection :- In Java, reflection allows us to inspect and manipulate classes, interfaces, constructors, methods, and fields at run time.

There is a class in Java named Class that keeps all the information about objects and classes at runtime.
The object of Class can be used to perform reflection.

What is the Java Reflection API?
1. Class Manipulator.
2. Used to manipulate classes and everything in a class.
3. can slow down a program because the JVM can't optimize the code.
4. Can't be used with applets.
5. Should be used sparingly.

Reflection of Java Classes

In order to reflect a Java class, we first need to create an object of Class.
And, using the object we can call various methods to get information about methods, fields, and constructors present in a class.

There exists three ways to create objects of Class:
1. Using forName() method
2. Using getClass() method
3. Using .class extension



Example 1:-
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.Class;

class vehicle
{
public void g()
{
System.out.println("I am Inside vehicle");
}
}
class car extends vehicle
{
public int x ;
public String name;
car()
{
System.out.println("i am inside constructor");
}
car(int x, int y)
{
System.out.println("Sum is " + (x+y));
}
public void j()
{
System.out.println("I am Inside car");
}
}
public class TestCsc3
{
public static void main(String args[]) throws ClassNotFoundException
{
car cr = new car();
Class cobj = cr.getClass();
TestCsc3 tc = new TestCsc3();
Class cobj2 = tc.getClass();
//get the name of class
String classname = cobj.getName();
System.out.println("Name of the class is " + classname);
//get super class
Class scla = cobj.getSuperclass();
System.out.println("Name of super class is " + scla.getName());
//get the constructor of the class
Constructor[] consname = cobj.getDeclaredConstructors();
for(Constructor c : consname)
{
System.out.println("Constructor name is :" +c.getName());
}
//get the modifier of the class
int modi = cobj2.getModifiers();
String modif = Modifier.toString(modi);
System.out.println("Modifier of class is " + (modif));
//get all the methods of the class
Method[] mth = cobj.getMethods();
for(Method m : mth)
{
System.out.println("Name is the methods" + m.getName());
}
//get declared methods of the class
Method[] mth2 = cobj.getDeclaredMethods();
for(Method m2 : mth2)
{
System.out.println("Name is the declared method is :-> " + m2.getName());
}
//get fields of class
Field[] fld = cobj.getDeclaredFields();
for(Field f : fld)
{
System.out.println("Name of the fiels is " + f.getName());
}
}
}

Output :-

i am inside constructor
Name of the class is practice.car
Name of super class is practice.vehicle
Constructor name is :practice.car
Constructor name is :practice.car
Modifier of class is public
Name is the methods j
Name is the methods g
Name is the methodswait
Name is the methodswait
Name is the methodswait
Name is the methodsequals
Name is the methodstoString
Name is the methodshashCode
Name is the methodsgetClass
Name is the methodsnotify
Name is the methodsnotifyAll
Name is the declared method is :-> j
Name of the fiels is x
Name of the fiels is name
-------------------------------------------------------------------------------------
Example 2:-

import java.lang.Class;
import java.lang.*;
class pent
{
}
public class Reflection2
{
public static void main(String[] args) throws ClassNotFoundException
{
Class c = Class.forName("practice.pent");
System.out.println("the name is class " + c.getName());
}
}

Output :-
the name is class practice.pent

No comments:

Post a Comment