wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""private" access" Watch ""private" access" New topic
Author

"private" access

Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
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

Joined: Jan 03, 2006
Posts: 490
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 ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Artemesia Lakener
Ranch Hand

Joined: Jun 21, 2005
Posts: 162
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

Joined: Jun 21, 2005
Posts: 162
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

Joined: Jan 03, 2006
Posts: 490
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
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
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
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jan 03, 2006
Posts: 490
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

Joined: Jan 17, 2006
Posts: 1296
You mean like this?


[Edit: everyone else beat me to the puncch lol]
[ February 21, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
As an additional comment, reflection allows you to access even the private members of a class.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "private" access
 
Similar Threads
Garbage collection query
Garbage collection...
hashmap with duplicate key.
TreeSet and compareTo Java 5
Why Object.clone() ?