jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Rules Roundup Question 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 "Rules Roundup Question" Watch "Rules Roundup Question" New topic

Rules Roundup Question

Jayant Raj
Ranch Hand

Joined: Mar 11, 2004
Posts: 42

I came across this question in the Rules Roundup game.

Can one object access a private variable of another object of the same class?

Answer : Yes

Please explain.. preferably with an example.

regards,<br />Jayant Raj<br /> <br />SCJP 1.4 [98%]<br />SCWCD 1.4 [91%]<br />SCBCD [In Progress]
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Access modifiers (private, default, protected, public) ... technically "default" isn't an access modifier since you don't type it out--but still!

Anyways, those just deal with visibility by the class, not an object. Check this out:

Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
"Private" means Private to the class, not to the object

class A{
private String xxx = "Blah!";

void testSomething(A obj){
obj.xxx = "valid"; // this is valid

MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Consider Paul's rocket mass heater.
subject: Rules Roundup Question
Similar Threads
Objects passed by reference
Accessing private variables?
rules roundup answer re: var types allowed in SWITCH statement
how much more should i get
The new beta test on Javaranch