This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

accessing private variables of thread class using reflection  RSS feed

 
Bhagirathi Mallick
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its ok, you can anser it
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!