my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes when private is not so private 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "when private is not so private" Watch "when private is not so private" New topic
Author

when private is not so private

Tom Adams
Ranch Hand

Joined: Feb 07, 2003
Posts: 56
This one caught me...

Program result?
1. Compile Error (have you seen line 1?)
2. Runtime Exception
3. Output is 100

#3! Seems like accessing another instance's (of your class) private variables is ok.


Tom
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
You are correct. A private member is private to the class but not private to an object instance.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
As long as the access expression to the private member (otherPrivate.i) occurs whithin the class (Private) declaring the private member (i), the access is allowed.


SCJP2. Please Indent your code using UBB Code
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202
Its is possible for the objects of the same class to access each others private methods because private member's is private to the class but not private to an object instance.


SCJP 1.4, SCWCD 1.4
Reshma Shanbhag
Ranch Hand

Joined: Sep 17, 2002
Posts: 202
Correction to the above post ...
Its is possible for the objects of the same class to access each others private methods/variables because private member is private to the class but not private to an object instance.
 
jQuery in Action, 2nd edition
 
subject: when private is not so private
 
Similar Threads
Overriding final methods
Can final method be overridden?
Please Help with the flow of this code
grammar help: what is this structure?
I need some calculation help