• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaRanch Rule Round-up Game?

 
Swapan Mazumdar
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Ranchers,
Can one object access a private variable of another object of the same class???
 
David Ulicny
Ranch Hand
Posts: 724
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it can't. Private varible is private for each instance of the same class.
 
Swapan Mazumdar
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had the same query long time back. Here is the explanation

Hope that helps...
 
Amit Das
Ranch Hand
Posts: 206
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Amit Das
Ranch Hand
Posts: 206
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Marc for clearing the misconception....neways the code is better understtod with a litle modification hereunder:



 
Swapan Mazumdar
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the bigger lesson is this: Test your doubts by writing code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic