aspose file tools *
The moose likes Java in General and the fly likes access private attributes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "access private attributes" Watch "access private attributes" New topic
Author

access private attributes

Simon Xu
Ranch Hand

Joined: Aug 16, 2000
Posts: 235
hi,
Is it legal for one object of a private class to access another object of the same class?
Thanks
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: May 05, 2000
Posts: 13974
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.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Aj Manch
Ranch Hand

Joined: May 07, 2001
Posts: 50
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Aj Manch
Ranch Hand

Joined: May 07, 2001
Posts: 50
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

Joined: May 04, 2001
Posts: 2
Objects of a given class ARE allowed to access private member variables of a different object of the same class.
Aj Manch
Ranch Hand

Joined: May 07, 2001
Posts: 50
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

Joined: Jan 07, 1999
Posts: 6920
It's general O-O behaviour, and it's vital so we are able to sensibly implement things like the "equals" method.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Simon Xu
Ranch Hand

Joined: Aug 16, 2000
Posts: 235
Thanks for all of you.
I originally meaned private members in a class.
Simon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: access private attributes