• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing a Private Variable.

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post some code example? I don't understand what you mean.
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
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 question long time back.. Check out the link below

Here

Hope that helps
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic