Site icon DataFlair

Reflection in Java | Java Reflection API Tutorial

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

In our previous Java tutorial, we looked at Iterators in Java. Today, in this article, we are going to discuss Reflection in Java. Here, we will see what Java Reflection is & how it can be utilized to get data.

Moreover, we will look at the pros and cons of Java reflection. Along with this, we will understand the sample code using Java reflection and its class.

So, let’s start Reflection in Java.

Reflection in Java | Java Reflection API Tutorial

What is Reflection in Java?

Reflection in Java is an Application Programming Interface(API) that is utilized at runtime to change classes, methods, and interfaces.

It is an API that is utilized to look at or change the behavior of methods, classes, and interfaces at runtime.

Do you know the Working of Java Packages?

Java Reflection- Reflection API

How to Get Data Using Java Reflection?

Reflection can be utilized to get data about :

Let’s revise Java Copy Constructor | Constructor Overloading in Java

import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.lang.reflect.Constructor;
class Test
{    private String s;
   public Test()  { s = "DataFlair"; }
   public void method1()  {
       System.out.println("The string is " + s);
   }
   public void method2(int n)  {
       System.out.println("The number is " + n);
   }
   private void method3() {
       System.out.println("Private method invoked");
   }
}
class Demo
{
   public static void main(String args[]) throws Exception
   {
       Test obj = new Test();
       Class cls = obj.getClass();
       System.out.println("The name of class is " +
                           cls.getName());
       Constructor constructor = cls.getConstructor();
       System.out.println("The name of constructor is " +
                           constructor.getName());
       System.out.println("The public methods of class are : ");
       Method[] methods = cls.getMethods();
       for (Method method:methods)
           System.out.println(method.getName());
       Method methodcall1 = cls.getDeclaredMethod("method2",
                                                int.class);
       methodcall1.invoke(obj, 19);
       Field field = cls.getDeclaredField("s");
       field.setAccessible(true);
       field.set(obj, "JAVA");
       Method methodcall2 = cls.getDeclaredMethod("method1");
       methodcall2.invoke(obj);
       Method methodcall3 = cls.getDeclaredMethod("method3");
       methodcall3.setAccessible(true);
       methodcall3.invoke(obj);
   }
}

Output 

The name of the class is Test

The name of constructor is

Test The public methods of class are:

method2

method1

wait

wait

equals

wait

toString

hashCode

getClass

notify

notifyAll

The number is 19

The string is JAVA

Private method invoked

Learn Singleton Class in Java – Two Simple Ways For Implementing

 Important points:

A Syntax for Java Reflection: get declared method–

Class.getDeclaredMethod(name, parametertype)
name- the name of method whose object is to be created
parametertype - parameter is an array of Class objects

invoke(): At runtime, to invoke a method of a class, we use the following method-

A Syntax for the invoke method-

Method.invoke(Object, parameter)

Java Wildcard – Types of Wildcard in Java

So, if the Java reflection class method doesn’t accept any parameter, then an argument is passed as null.

Advantages of Java Reflection –

Disadvantages of Java Reflection –

Learn about Collection Framework in Java – Hierarchy, Need & Advantages

Security Considerations:

Java Reflection is a powerful tool that can be used to bypass access modifiers and invoke private methods or access private fields. Therefore, this can be beneficial in certain situations, such as unit testing frameworks that need to access private members for testing purposes. However, it’s important to be aware of the security implications. Therefore, Malicious code could potentially exploit reflection to access or modify sensitive data that would normally be hidden.

However, to mitigate these risks, it’s generally recommended to avoid using reflection in production code unless necessary. Therefore, when using reflection, it’s advisable to implement proper security checks to ensure that only authorized code can access restricted members

Conclusion

Hence, we have a complete understanding of reflection in Java. Along with this, we saw the advantages and disadvantages of Java Reflection. At last, we learned the Java reflection class and java reflection invokes a method with the help of an example. Furthermore, if you have any queries, feel free to ask through the comment section.

For reference

Exit mobile version