Java Reflection API: All You Need To Know

Published on Jul 19,2019 1.7K Views

Java Reflection API: All You Need To Know

edureka.co

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 following pointers:

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:

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-

No let us take a look at Java Reflection API methods,

Methods used in java.lang.Class

Method

Description

public String getName()

returns the class name

public static Class forName(String className)throws ClassNotFoundException

loads the class and returns the reference of Class class.

public Object newInstance()throws InstantiationException,IllegalAccessException

creates new instance.

public boolean isInterface()

checks if it is interface.

public boolean isArray()

checks if it is array.

public boolean isPrimitive()

checks if it is primitive.

public Class getSuperclass()

returns the superclass class reference.

public Field[] getDeclaredFields()throws SecurityException

returns the total number of fields of this class.

public Method[] getDeclaredMethods()throws SecurityException

returns the total number of methods of this class.

public Constructor[] getDeclaredConstructors()throws SecurityException

returns the total number of constructors of this class.

public Method getDeclaredMethod(String name,Class[] parameterTypes)throws NoSuchMethodException,SecurityException

returns the method class instance.

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

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

Disadvantages of using Java Reflection API

Thus we have come to an end of this article on ‘Java Reflection API’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this article  and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial