File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes accessing private variables of thread class using reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "accessing private variables of thread class using reflection" Watch "accessing private variables of thread class using reflection" New topic
Author

accessing private variables of thread class using reflection

Bhagirathi Mallick
Greenhorn

Joined: Jul 21, 2011
Posts: 26
why we cannot access private variable named as "threadstatus" of the class Thread in out side of that class
when i call accessThreadStatus method it generates one exception



And if the above happens , then how to make a private variable not accessible to the out side class.
please any one find the solution
Thanks in advance.
Bhagirathi Mallick
Greenhorn

Joined: Jul 21, 2011
Posts: 26
its ok, you can anser it
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18532
    
  40

Bhagirathi Mallick wrote:why we cannot access private variable named as "threadstatus" of the class Thread in out side of that class
when i call accessThreadStatus method it generates one exception




And if the above happens , then how to make a private variable not accessible to the out side class.
please any one find the solution
Thanks in advance.


It generates an exception because you are trying to access a private variable -- by default, reflection does not allow to access a private variable. To enable access, you need to call the setAccessible() method, which you other example has done.... Now, if you are asking why the other example doesn't work, it could be the security manager. The setAccessible() method is prevented from being called in most cases -- applets, RMI, etc. -- try running it as a stand alone application where there isn't a security manager by default.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
wood burning stoves
 
subject: accessing private variables of thread class using reflection
 
Similar Threads
private access through reflection
how to read string value using java reflection
Need help on JTree
how to invoke a method of a reflected field
Getting value of static variable with Reflection ?