Accessing Private Members of a Java Class using Reflection

Accessing Private Members of a Java Class using Reflection

Java Reflection API provides a feature to access private members of a Java Class.

java.lang.reflect package provides the AccessibleObject class that is parent class of Constructor, Method, and Field class. Using the AccessibleObject class, we can change the access control flags of a Reflection Object

Lets understand how to achieve this with below example.

Class Having Private Members[MyClass.java]

Class to Access the private members of the above class using Reflection[MainClass.java]

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.