jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Private member access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Private member access" Watch "Private member access" New topic
Author

Private member access

VarunKumar Mallisetty
Ranch Hand

Joined: Jun 18, 2006
Posts: 31
I am not able to understand what is actually meant by the stamement
"An object can access the private members of another object of same type"

The above code gives a complier error saying that i has private access at b.i
That's fine..but when for the following code there is no error.In previous case when iam not able to access the private member of class B how come i'm able to access the private member of A.Here also the reference to the object is only passed and how come the private members are accessed.I'm a bit confused.I know that code inside a class can only access the private members but can't understand accessing private members using "." operator.

I hope my doubt is clear


SCJP 6.0
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The key phrase in the quote you showed is "of the same type." Any code in class A can access the private members of any instance of class A. But code in class A cannot access the private members of any instance of class B, because B is a different type (a different class.) Make sense?


[Jess in Action][AskingGoodQuestions]
Zip Ped
Ranch Hand

Joined: Jul 26, 2005
Posts: 336
In the first case the Class B is a different class than Class A. So the vairable "b.i" is not visible.

For the second case - objects can see the private variables and methods of an object of the same type. The private keyword is class-private, not object-private.


More on this here.
VarunKumar Mallisetty
Ranch Hand

Joined: Jun 18, 2006
Posts: 31
Ernest,Thanks for the reply. It does make sense, but i wanted to know how it is acutally implemented rater than taking it as a rule and remembering. I don't know if iam asking too much of details.
When i posted the question my intention was quite similar to the question which is present in link sent by kailash.

Kailash, Thanks a lot. The link you provided is very nice. I understood that private means "private to class" and not to objects. But, is there anyway we can know implementation details of private variables etc.
vivien siu
Ranch Hand

Joined: Nov 10, 2005
Posts: 143
Hello,

can someone explain what's the difference of class-private and object-private?


I'm not available, my BF's name is WORK.
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by vivien siu:
Hello,

can someone explain what's the difference of class-private and object-private?


Well as this question is itself very abstract but what I can tell is, Class-Private means the members of that class are private because of that you cann't access those members outside of that class.

And about Object-Private there is nothing like that Object also some public members and private members, actually this depends upon on Class. But Yes If any member of class is declared private then you cann't access those members outside of that class, even if you are creating Object of that class anywhere outside. because visibility of that member is in that class itself.

Hope it clears you.

vivien siu
Ranch Hand

Joined: Nov 10, 2005
Posts: 143
thanks Ankur, but from your explanation, class-private and object-private sounded almost the same

class-private:
Class-Private means the members of that class are private because of that you cann't access those members outside of that class.


object-private:
If any member of class is declared private then you cann't access those members outside of that class, even if you are creating Object of that class anywhere outside. because visibility of that member is in that class itself.
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by vivien siu:
thanks Ankur, but from your explanation, class-private and object-private sounded almost the same

class-private:



No vivien As I told you that their is nothing like any Object-Private word... That was the explanantions what you can understand in terms of technology.

Access Modifiers exists in terms of Class.

And if you create an Object of that class, that doesn't means that if you create a public Object then you can be able to access the private members. Because it's in the other class, and private members are no more visible outside that particular class.

So that doesn't matters whether you create a public Object or Private Object for accessing Class members.

Hope it clears You. Revert back if any concern.



vivien siu
Ranch Hand

Joined: Nov 10, 2005
Posts: 143
when you say class-private, it means private methods and variables of the class.

and object-private means: private Object obj = new Object(); right?

ok now I get it, thanks!
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Exactly That's what I really want to describe.


VarunKumar Mallisetty
Ranch Hand

Joined: Jun 18, 2006
Posts: 31
Well, offcourse it does not matter if we create a public object or private object of a class to access the members of the class.
But what i'm trying to understand is "how" the members variables of the object are being cabable of access.
According to my knowledge, all the constructors and methods of a class(except static methods) are given a reference to the object of that class, which we call as "this".
Using "this" a class can access the members as this.mem_name or since this is implicit we directly use the mem_name in the methods. But "this" only refers to the that particular Object. "How" come we are able to access the members as A.i in the code above, when that is not possible out side the definition.
I did clearly understand that private means class-private and that members of same object can be acessed.What i can't understand is "how" by augmenting with my knowledge about accessing members with "this".
I would be very happy with a different answer than to state the rules again and say "that's the way it works".
 
 
subject: Private member access