• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"private" access

 
Artemesia Lakener
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A {
private String name;
...

public A(String name) {...}
public A add(A a) {
return new A(name + a.name);
}
}

class Test {
void test() {
A a = new A("john");
A a1 = new A("stve");
A a2 = new A("Loi");
A a3 = a1.add(a2);
System.out.println("A.name = " + a.name); // marked line
}
}

1. Everyone knows it fails in compilation because of "a.name". But, from the book, it says -- "A private variable or method may only be used by an instance of the class that declares the var or method". So, isn't "a" an instance of class A ?? It seems it satisfies the condition, why does compiler complain ??

2. why "return new A(name + a.name)" in the class A works ? i.e why can we use "a.name" here ??
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. a is a reference to the object not an instance of an object. That is a very important distinction. With a private variable in a class, only an instance of that class can directly access it.

2. First off, you do not need to pass a reference to itself. All it needs to do is call name, because add is a member of A. But the reason it works is because it is inside the class that owns name, if that makes sense. However, it is bad programming to do something like that. Proper use of mutators/acessors is more elegant.
[ February 21, 2006: Message edited by: Rusty Shackleford ]
 
Artemesia Lakener
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rusty Shackleford:
1. a is a reference to the object not an instance of an object. That is a very important distinction. With a private variable in a class, only an instance of that class can directly access it.

But that's how java access an "instance" -- always use a reference to do it. For example, when you do "MyClass mc = new MyClass(); mc.doThis();" isn't "mc" a reference too ? I don't know how you directly play and use an "instance" as you said ? Can you show me a concrete example that you use an "instance" directly and then use this "instance" to access its private variable ?

2. First off, you do not need to pass a reference to itself. All it needs to do is call name, because add is a member of A. But the reason it works is because it is inside the class that owns name, if that makes sense. However, it is bad programming to do something like that. Proper use of mutators/acessors is more elegant.

So you mean if it is "in the same class", then that's ok, otherwise it is not ok ? But that's not what the definition says. The definition only says "private variable can only be accessed by its instance. It doesn't say anything about "in the same class" or not. Can you provide me an example that's "not in the same class but use its instance to access private variable directly" ?

Bear with me for the questions.

[ February 21, 2006: Message edited by: Rusty Shackleford ]
 
Artemesia Lakener
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rusty Shackleford:
1. a is a reference to the object not an instance of an object. That is a very important distinction. With a private variable in a class, only an instance of that class can directly access it.


But that's how java access an "instance" -- always use a reference to do it. For example, when you do "MyClass mc = new MyClass(); mc.doThis();" isn't "mc" a reference too ? I don't know how you directly play and use an "instance" as you said ? Can you show me a concrete example that you use an "instance" directly and then use this "instance" to access its private variable ?


2. First off, you do not need to pass a reference to itself. All it needs to do is call name, because add is a member of A. But the reason it works is because it is inside the class that owns name, if that makes sense. However, it is bad programming to do something like that. Proper use of mutators/acessors is more elegant.

[ February 21, 2006: Message edited by: Rusty Shackleford ]


So you mean if it is "in the same class", then that's ok, otherwise it is not ok ? But that's not what the definition says. The definition only says "private variable can only be accessed by its instance. It doesn't say anything about "in the same class" or not. Can you provide me an example that's "not in the same class but use its instance to access private variable directly" ?

Bear with me for the questions.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A private variable can only be accessed(directly) by its instance. A reference is a reference to an instance, not the instance itself. When I said class, I meant instance, I was half-asleep.

The way you are reading it would totally violate encapsulation. It would allow one class to directly access the private member of another object.


Using a similar example



Class2:



Yes, I know that in this case toString is redundant, but so is a String wrapper class.

Another way to think of it is look at what is going on in memory.

Class2 c2 = new Class();

Class2 c2 <- this is the reference variable

new Class2() <- this creates the object

= <- this links( or points) the reference to the object

c2 is a reference that lives on the heap inside the memory allocated for c1, which was instantiated elsewhere.

The object c2, also lives on the heap, but has its own memory and nothing outside that memory can directly access private instance level(as opposed to class method level) variables, including the c2 reference, because c2 lives elsewhere.
[ February 21, 2006: Message edited by: Rusty Shackleford ]
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has been a long while since I did this... as this is *not* a programming technique that I use.

But if I remember correctly, an object can indeed access the private variables of another object, if the two objects are of the same class.

Henry
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have 2 objects, and the member variables are private and non-static, they have their own copies which can have different values. Since they do not share the same memory space, the usual encapsulation rules apply.

If it is static, they can not only access it, but they share the SAME variable, so if one object changes it, the value is changed for all of them.
 
Henry Wong
author
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, I wasn't refering to static variables. I think an instance can access the private variable of another instance, if both are of the same class type.

Don't have a compiler at the moment, so hopefully, someone can test this. I really would like to know if I am wrong about this.

Henry
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, in order to refer to the private member of an instance, you do use the instance name. However, from within a class of the same type, you can refer to the private members.

For example, in the methods in this class definition, we can directly refer to the private variable of another instance of class A.



However, from a class of a different type, we cannot refer to the private members of other classes.

This class definition causes a compile-time error because we cannot access the private member of class A from within class B.

 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You were right, Henry Wong , that is frikken odd that it would be allowed. I stand corrected. Learn something new everyday, although I am at a loss why someone would want to use it. Here is what I wrote real fast to test it






It printed out
5
55

Back to the OP, another way to think about it is if you can not access it with the this keyword then it is not visible to the calling object. The code above I put this line in Test, and predictably, the compiler screamed at me.

System.out.println(t1.val);
[ February 21, 2006: Message edited by: Rusty Shackleford ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean like this?


[Edit: everyone else beat me to the puncch lol]
[ February 21, 2006: Message edited by: Garrett Rowe ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an additional comment, reflection allows you to access even the private members of a class.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic