Java: Accessing private fields directly from another instance of the same class?

No, it's not. The reason that private variables and methods are not accessable from other classes is to allow you to change the internals of your class without having to change all the code that uses the class (that and to prevent the user of your class from e.g. Setting a variable to a value that it's never supposed to have).

No, it's not. The reason that private variables and methods are not accessable from other classes is to allow you to change the internals of your class without having to change all the code that uses the class (that and to prevent the user of your class from e.g. Setting a variable to a value that it's never supposed to have). If you use private variables of other objects that doesn't hurt anything, because if you'd restructure your class's internals, you'd have to change the code inside the class anyway.

Just because it will work 99% of the time doesn't mean it's the right thing to do. It breaks encapsulation and couples the classes more tightly for no other reason than saving typing five characters. Please see my answer.

– dj_segfault Oct 11 '09 at 4:21 1 "couples the classes more tightly together" - which classes? There is only one class concerned here, and you can't "uncouple" a class from itself. – sepp2k Oct 11 '09 at 11:16 Not necessarily.

The parameter passed to equals() is not necessarily the exact same class. – dj_segfault Oct 13 '09 at 14:04 ^ you mean, ClassCastException? ;) – Zefi Feb 8 '10 at 15:44.

I tend to always use getters, because sometimes a getter isn't just "return(foo)". Sometimes they initialize things if they're null, or have some debug logging in them, or validate the current state in some way. It's more consistent.

I don't think this is bad practice, but a feature of the language. It not only allows you to test equals the way you do, but it is also useful in a Prototype pattern for object creation.

That is fine and completely normal. It is a little bit odd to think that this can fiddle with the private fields of other, but it's okay because there's no way anything bad can happen as far as some third party being able to muck with an Odp object's internals. Any method of the Odp class can modify any private members of any Odp object, even non-this ones, but that's fine since any such methods can obviously be trusted!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions