Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

access private attributes

 
Simon Xu
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Is it legal for one object of a private class to access another object of the same class?
Thanks
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Private means that the object cannot be modified or viewed directly by classes outside their own class; they can only be modified or viewed by methods of that same class.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Simon Xu:
Is it legal for one object of a private class to access another object of the same class?
Absolutely it is legal. In fact, one object of a class will have access to the private attributes of another object of the same class.
 
Aj Manch
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Simon Xu:
Is it legal for one object of a private class to access another object of the same class?

I believe the question is one of private CLASS and not about private ATTRIBUTES. (I still don't understand your answer, which I have mentioned at the bottom). You cannot declare a class as private. You can have that effect by declaring the constructor of that class as private. In that case you would have to have a static method on this class which can create an instance of it. (This forms the basis to Singleton pattern of creating object.) You can also have a public method on this class which accepts an object of the same class. Is that what you are looking for?
Originally posted by Thomas Paul:
Absolutely it is legal. In fact, one object of a class will have access to the private attributes of another object of the same class.

How is this possible?? Each object has its own memory space and the private attributes cannot be directly accessed by any method outside the object even if the other object is also of the same type. Am I missing anything here?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the original questioner was confused, asking about private attributes in the subject line, and private classes in the text of the message. I don't know which one he really meant, but either way the answer is yes, it's possible.
For a private class - this would necessarily be a nested class, either a static or non-static member class. Within the definition of that class though, it is of course possible to refer to other objects of the same class. It's only outside the enclosing class that the private nested class becomes inaccessible.
For a private member:
> How is this possible?? Each object has its own memory space
> and the private attributes cannot be directly accessed by any
> method outside the object even if the other object is also of
> the same type. Am I missing anything here?
You need to reread the rules for the "private" keyword, or test its use with a compiler. A private member cannot be accessed outside its class - but nothing says it can't be accessed from another object of the same class. The private keyword is used to prevent private code from being exposed to outside code. Two objects of the same class necessarily share the same code, so this is not an issue. This was a design choice by the creators of Java - they could have required that compilers prevent access from outside objects as well as outside classes, but they saw no benefit in doing so, so they didn't.
 
Aj Manch
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Well, the original questioner was confused, asking about private attributes in the subject line, and private classes in the text of the message. I don't know which one he really meant, but either way the answer is yes, it's possible.
For a private class - this would necessarily be a nested class, either a static or non-static member class. Within the definition of that class though, it is of course possible to refer to other objects of the same class. It's only outside the enclosing class that the private nested class becomes inaccessible.
For a private member:
> How is this possible?? Each object has its own memory space
> and the private attributes cannot be directly accessed by any
> method outside the object even if the other object is also of
> the same type. Am I missing anything here?
You need to reread the rules for the "private" keyword, or test its use with a compiler. A private member cannot be accessed outside its class - but nothing says it can't be accessed from another object of the same class. The private keyword is used to prevent private code from being exposed to outside code. Two objects of the same class necessarily share the same code, so this is not an issue. This was a design choice by the creators of Java - they could have required that compilers prevent access from outside objects as well as outside classes, but they saw no benefit in doing so, so they didn't.

While what you said is true for methods it is not for attributes. The attributes for different objects can hold different values and there is no way that private attributes can be referred outside that object unless one of the accessible methods exposes it.
 
Simon Baker
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects of a given class ARE allowed to access private member variables of a different object of the same class.
 
Aj Manch
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Simon Baker:
Objects of a given class ARE allowed to access private member variables of a different object of the same class.

Umhhhh. This is indeed true. Can't believe I missed it. Thanks guys for pointing that out.
Is this Object Oriented Programming behavior or is it specific to Java?
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's general O-O behaviour, and it's vital so we are able to sensibly implement things like the "equals" method.
 
Simon Xu
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all of you.
I originally meaned private members in a class.
Simon
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic