http://www.girishgaurav.com/js/ga_social_tracking.js

«

»

Jun 21

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

Skip to comment form

  1. Darcy Kearbey

    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.

  2. Dzieci

    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.

  3. concord replica watches

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

  4. Replicaonline

    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!

  5. mantar hastalığı

    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.

  6. Grzegorz Grzybek

    Or you can use Spring:

    ReflectionTestUtils.getField(object, “fieldName”)

    🙂

    1. girishgaurav

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

Leave a Reply

%d bloggers like this: