• 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

Access Modifiers question on Rule's Roundup

 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I came across the blow question as I was playing "Rules Roundup" in JavaRanch.

"Can one object access a private variable of another object of the same class?"

The answer that i got was as below.

Answer : Yes
Private means "private to the class", NOT "private to the object". So two objects of the same class could access each others private data.

I am not quit clear about the answer and the question too. After instantiating two different values how can one object access the other objects data.

can anyone please provide a explanation.

Thank You,
Poorna
 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's an example. I sometimes write a so-called "copy constructor" to create a new object based on an existing object of the same type. To instantiate its own fields, it needs access to the fields of the other object. Normally, you would access these fields through getters, but since the objects are of the same type, you can access the fields directly:
reply
    Bookmark Topic Watch Topic
  • New Topic