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 {

privateint privateInt = 10;

private String privateString = “temp String”;

privatelong privateLong = 1234567890L;

privatevoid resetFields(int i, String s, long l) {

privateInt = i;

privateString = s;

privateLong = l;

}

publicvoid display() {

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

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

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

}

}

class Test {

/**

* @param args

*/

publicstaticvoid 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));

Like this:

Related

7 comments

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.

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.

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.

Categories

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 119 other subscribers

Email Address

Donate

Developing this blog took a lot of effort and time, months and months of continuous voluntary unpaid work. If you like this blog or if you are using it for commercial purpose, please consider a donation to the developer to help support future updates and development.