aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JavaRanch Rule Round-up Game? 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 "JavaRanch Rule Round-up Game?" Watch "JavaRanch Rule Round-up Game?" New topic
Author

JavaRanch Rule Round-up Game?

Swapan Mazumdar
Ranch Hand

Joined: Jul 23, 2003
Posts: 83
To Ranchers,
Can one object access a private variable of another object of the same class???
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
No, it can't. Private varible is private for each instance of the same class.


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

Joined: Jul 23, 2003
Posts: 83
Originally posted by David Ulicny:
No, it can't. Private varible is private for each instance of the same class.

But I got the following comment when I opted for "No"
Yes,
Private means "private to the class" not "private to the object". So two objects of the same class could access each other's private data.


What should we conclude with???
sri rallapalli
Ranch Hand

Joined: Mar 15, 2005
Posts: 88
Hi,
can you please tell me what is private to object.
i think a private member variable in a class is private to an object, but you said it is private to class only,
then tell me what kind of data will become private to an object.
Swapan Mazumdar
Ranch Hand

Joined: Jul 23, 2003
Posts: 83
Originally posted by sri rallapalli:
...
i think a private member variable in a class is private to an object, but ...

I am also with you but this link below has something else to say,
JavaRanch Rule Round-up Game
You may please visit the link. It has 161 fun-packed mind twisters. But I found this particular question a bit weird.
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
I had the same query long time back. Here is the explanation

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.
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
But Jay,

according to the link u've provided, the two Employee objects have been made to access their pravate variables in their own class. If there we'd have created these objects in some different class, then definately it wasn't possible so in that case it goes wrong....but there one might say that as per the defination of 'private' that it's in a class and hence first of all if we're going out of the very class we don't have anything left to debate.
So two objects of same class accessing their private variables is only possible in the same class...

thanx
amit
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343



"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
Thanx Marc for clearing the misconception....neways the code is better understtod with a litle modification hereunder:



Swapan Mazumdar
Ranch Hand

Joined: Jul 23, 2003
Posts: 83

I thank you all who really helped me in clearing the hard-to-go wrong understanding.

-Swapan
[ March 24, 2005: Message edited by: Swapan Mazumdar ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I think the bigger lesson is this: Test your doubts by writing code.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JavaRanch Rule Round-up Game?