This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes private access through reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "private access through reflection" Watch "private access through reflection" New topic
Author

private access through reflection

venkat ga
Greenhorn

Joined: Feb 14, 2010
Posts: 7
Hi,

Can anybody explain below program. private means only with in the scope. but outside we can't access.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8801
    
    5
Interesting topic, and *maybe* useful for the SCJP, but I think this will cause more confusion than help in this, SCJP, forum so I'm moving it to the General forum.

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18508
    
  40

venkat ga wrote:
Can anybody please explain below program. private means only with in the scope. but outside we can't access.


See these lines....



It means turn off access checks. For Applets, or anything else with a security manager, this call will generate a security exception. For everything else, it will allow a program to access all fields (regardless of access permission) of an object.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
venkat ga
Greenhorn

Joined: Feb 14, 2010
Posts: 7
Thank you very much Henry,

I changed in the same code flags true to false,



I got exception as

NoNameTitle:NoNameAuthor:100
Exception in thread "main" java.lang.IllegalAccessException: Class simple.Demo can not access a member of class simple.Book with modifiers "private"


Turn off access checks : It means internally JVM treats that, all are public ?
So we can access any where irrespective of access specifiers once we turn off access checks.



 
 
subject: private access through reflection
 
Similar Threads
Shadowing
My Array
A question about "equals"
Code not compiling.
Creating classes