• 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

from Java Rule Round-up, question #208

 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is as follows:
<code>
Can one object access a private variable of another object of the same class?
</code>
The answer:
<code>
Yes
Private means "private to class", NOT "private to the object". So two objects of the same class could access each others' private data.
</code>
Can someone give me more explanation?
Guoqiao
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
It's simple..do not get confused...
Private variables are accessible for a class in which it is declared..
Private variables can NOT be accessed/seen from the subclass or from any other package or from within a package..
So..lets see an example..

in the above code, since a1 , a2 are objects of BankAccount class..can freely access all the variables of that class ,including private variables...it's like they are two childs of One parent..
 
Guoqiao Sun
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your explanation, swati.
Guoqiao

Originally posted by swati bannore:
HI,
It's simple..do not get confused...
Private variables are accessible for a class in which it is declared..
Private variables can NOT be accessed/seen from the subclass or from any other package or from within a package..
So..lets see an example..
in the above code, since a1 , a2 are objects of BankAccount class..can freely access all the variables of that class ,including private variables...it's like they are two childs of One parent..


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic