| 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.
|
 |
 |
|
|
subject: when private is not so private
|
|
|