Forums Register Login

accessing private variables of thread class using reflection

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
its ok, you can anser it
1
+Pie Number of slices to send: Send
 

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
He baked a muffin that stole my car! And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1694 times.
Similar Threads
private access through reflection
how to read string value using java reflection
how to invoke a method of a reflected field
Need help on JTree
Getting value of static variable with Reflection ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:13:06.