Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

JavaRanch Rule Round-up Game?

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To Ranchers,
Can one object access a private variable of another object of the same class???
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had the same query long time back. Here is the explanation

Hope that helps...
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the bigger lesson is this: Test your doubts by writing code.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic