Access private fields and methods using reflection in java

In this post, we will see how to access private fields and methods using reflection in java.


Can you access private fields and methods using reflection?
Yes, you can. It is very easy as well. You just need to call .setAccessible(true) on field or method object which you want to access.

Access private field

Class.getDeclaredField(String fieldName) or Class.getDeclaredFields() can be used to get private fields.

💡 Did you know?

Class.getField(String fieldName) and Class.getFields() return public fields only, so you won’t be able to get private fields with them

Let’s see this with the help of example:
Consider a class named Employee which consists of two private fields name and age.

Create main class named PrivateFieldReflectionMain

When you run above class, you will get below output:
Output:

Name of Employee:John

As you can see, we are able to access private field name using reflection.

Access private method

If you want to invoke any method using reflection, you can go through invoke method using reflection.

Class.getDeclaredMethod(String methodName,Class<?>... parameterTypes) or Class.getDeclaredMethods() can be used to get private methods.

💡 Did you know?

Class.getMethod(String methodName,Class<?>... parameterTypes) and Class.getMethods() return public methods only, so you won’t be able to get private methods with them

Let’s see this with the help of example:
We will access Employee’s private method getAge() using reflection.

Create main class named

When you run above class, you will get below output:
Output:

Age of Employee: 33

As you can see, we are able to access private method getAge() using reflection.
That’s about how to access private fields and methods using reflection in java

Related Posts

  • 15 May

    Get and set Fields using reflection in java

    In this post, we will see how to get and set fields using reflection in java. java.lang.reflect.Field can be used to get/set fields(member variables) at runtime using reflection. Get all fields of the class All fields of the class can be obtained from the Class object. Here is an example. [crayon-600902218f504310794055/] Field[] will have all […]

  • 15 May

    Invoke constructor using Reflection in java

    In this post, we will see how to invoke constructor using reflection in java. You can retrieve the constructors of the classes and instantiate object at run time using reflection. java.lang.reflect.Constructor is used to instantiate the objects. Instantiate Constructor with no parameters In case, you want to create object using no args constructor at runtime, […]

  • 24 October

    Invoke Getters And Setters Using Reflection in java

    In this post, we will see how to call getters and setters using reflection in java. We have already seen how to invoke method using reflection in java. There are two ways to invoke getter and setter using reflection in java. Using PropertyDescriptor You can use PropertyDescriptor to call getters and setters using reflection. Getter: […]

  • 23 September

    How to invoke method using reflection in java

    In this post, we will see how to invoke the method using reflection in java. Let’s understand this with the help of the example. Create a class named Employee.java. We will invoke this class’s method using reflection. [crayon-600902218f929346481996/] Create a main method named EmployeeReflectionMain.java. [crayon-600902218f92c609602674/] When you run above program, you will get below output: […]

  • 23 September

    Java Reflection tutorial

    Introduction Reflection, as we know from the common term, is an image of yourself or something else you see in a mirror or a mirrored surface. Java, being an object-oriented language draws heavily from real-life examples and so is the Reflection API of Java inspired from real life. In Java, the process of analyzing, viewing […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.