Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: when private is not so private
 
Similar Threads
Can final method be overridden?
grammar help: what is this structure?
Please Help with the flow of this code
I need some calculation help
Overriding final methods