Accessing Private Members of a Java class using Reflection



Somebody asked me that, “Is there any way of accessing private members of a Java Class?

Then I got the idea to write this article, because Java Reflection API provides a feature for accessing 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 (Reflection is a feature in the Java programming language. It allows an executing Java program to examine or “introspect” upon itself, and manipulate internal properties of the program. For example, it’s possible for a Java class to obtain the names of all its members and display them.), and the setAccessible(boolean) method is used to change the access control flag of an Object.

package test;

/**

  • @author girish.gaurav

*

*/

class DemoClass {

private int privateInt = 10;

private String privateString = “temp String”;

private long privateLong = 1234567890L;

private void resetFields(int i, String s, long l) {

privateInt = i;

privateString = s;

privateLong = l;

}

public void display() {

System.out.println(“Int = ” + privateInt);

System.out.println(“String = ” + privateString);

System.out.println(“Long = ” + privateLong);

}

}

class Test {

/**

* @param args

*/

public static void main(String[] args) {

DemoClass obj = new DemoClass();

obj.display();

Class clazz = obj.getClass();

try {

Field intField = clazz.getDeclaredField(“privateInt”);

Field strField = clazz.getDeclaredField(“privateString”);

Field longField = clazz.getDeclaredField(“privateLong”);

intField.setAccessible(true);

strField.setAccessible(true);

longField.setAccessible(true);

System.out.println(“value of privateInt field is : ” + intField.get(obj));

System.out.println(“value of privateString field is : ” + strField.get(obj));

System.out.println(“value of privateLong field is : ” + longField.get(obj));

intField.set(obj, 100);

strField.set(obj, “this is new string”);

longField.set(obj, 55555L);

obj.display();

} catch (SecurityException e) {

e.printStackTrace();

} catch (NoSuchFieldException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

try {

Method method = clazz.getDeclaredMethod(“resetFields”, int.class, String.class, long.class);

method.setAccessible(true);

method.invoke(obj, 25, “new String from resetField method.”, 987654321L);

obj.display();

} catch (SecurityException e) {

e.printStackTrace();

} catch (NoSuchMethodException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

}

}

References:

Java Reflection API

 

7 comments

7 pings

Skip to comment form

  1. I simply want to tell you that I am very new to blogging and really loved you’re blog. Very likely I’m planning to bookmark your blog . You surely come with fabulous article content. Thank you for revealing your website page.

    • Dzieci on November 15, 2011 at 11:15 PM

    Good beat ! I would like to apprentice while you amend your internet site, how can i subscribe for a blog web site? The account aided me a acceptable deal. I’d been a little bit familiar of this your message provided bright clear idea.

  2. Read you article, make me have a deeper understanding to life, it is different meaning for me.

  3. IЎЇm impressed very informative, you did well in your blog. Pipe thank your for your blog, support you, hope you have a great day!

  4. I loved as much as you will receive carried out right here. The sketch is tasteful, your authored subject matter stylish. nonetheless, you command get got an nervousness over that you wish be delivering the following. unwell unquestionably come further formerly again since exactly the same nearly very often inside case you shield this hike.

    • Grzegorz Grzybek on June 24, 2010 at 2:48 PM

    Or you can use Spring:

    ReflectionTestUtils.getField(object, “fieldName”)

    🙂

    1. yes, you are absolutely right Grzegorz. if we are using Spring then we can do it using ReflectionTestUtils class.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: