aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Accessing a Private Variable. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Accessing a Private Variable." Watch "Accessing a Private Variable." New topic
Author

Accessing a Private Variable.

Mohit Agarwal
Ranch Hand

Joined: Mar 30, 2004
Posts: 88
Can one object access a private variable of another objet of the same class?
This was on the rules game .
The ans is yes. The reason is private to the class and not to the object so two objects can access each others private data.

I am confused pls clarify it.

Mohit Agarwal.
Would Be SCJP.


The will to win is worthless if you do not have the will to prepare.
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Can you post some code example? I don't understand what you mean.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
Lets use the following object as an example:




Let's look at the equals method above. If the obj reference is null (no object) no match.. return. If the obj is an instance of person or the object passed in is a person then we compare.

Since I am the person object (this class file) I know how to compare Person objects. We are the "same" class, therefore I am able to access the private members.

Now, if we had Employee extend Person, Exmployee would NOT be able to see the private members. Why? Because Employee is NOT a person. Employee extends a Person.

Hope this helps.


SCJP 1.4 SCWCD 1.4
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
I had the same question long time back.. Check out the link below

Here

Hope that helps


Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Accessing a Private Variable.