Phillip Lord <> scribbled the following:
>>>>>> "Joona" == Joona I Palaste <> writes:
> Joona> CJ <> scribbled the following:
> >> Hi,
> >> Came across this issue - anyone know why this works?
> >> public class Cat { private int privateVal; public void test() {
> >> Cat c1 = new Cat(); c1.privateVal = 1; } }
> >> Surely the "privateVal" variable should not be accessible at this
> >> point (should be out of scope)? Anyone care to enlighten me??
> >> Thanks
> Joona> It's a bug/feature (choose one) in Java that objects in the
> Joona> same class can access each other's private variables. It's
> Joona> not a scope issue but a visibility issue. IMHO it breaks the
> Joona> "encapsulation" idea but it can solve a lot of practical
> Joona> problems.
> Just depends how you look at it. Variables are private to the class,
> and not the object. In this case you are calling the private variable
> from the same class.
I would have to disagree with the notion "variables are private to the
class, and not the object". If they're not private to the object, then
how come each object gets its own copy of them, and can alter them to
its heart's content without disturbing the other objects of the same
class? Huh?
> It makes sense to me. The point of privacy is to say "you should only
> be changing this if you know what you are doing". If you have
> implemented Cat, then you should know what you are doing with all
> objects of Cat.
I'd take that point of privacy one level further, and allow one single
*object* to only modify itself willy-nilly. After all, objects are a
more usable concept than classes, which can be shown by the existence
of languages with objects but not classes, whereas I don't know any
with classes but not objects.
--
/-- Joona Palaste () ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
|
http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"I am lying."
- Anon