"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
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 ]
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.
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
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter