Private members in Java

Are private members in Java distinct from other members of a private programming language? Because I was surprised that I could just pass an object of some type for comparison, and then access the private members of this object without using any getMethod method.

eg.

public class Foo implements Comparable<Foo>{ private int bar; public Foo() { bar = 1; } public int compareTo(Foo o) { if(bar == o.bar) return 0; return 1; } } 
+4
source share
4 answers

Despite the fact that I have been developing Java for life for 11 years, I was terrified when I discovered this fact two (!) Years ago. I must have read this before, but somehow it never sank. I agree that at first it seems strange, but when you think about it a little longer, it makes sense.

What is OO? About sharing problems and hiding information, right? Therefore, in your design you should always (should) try to avoid "knowing" about things outside of your own care, because this is tantamount to assumptions; what you know "at design time may change later and invalidate the assumption that your class makes.

In this case, you allow Foo to access another private Foo panel that first feels β€œaware” of something out of focus. But since we are talking about two instances of the same class (Foo), this is actually not beyond our concern! If, for example, we change the bar from int to double, we do it in one class, and any compilation errors will be displayed immediately.

Also imagine that this type of access was denied. So what if Foo called his own compareTo, passing 'this' as an argument? Then we would have to forbid Foo to access his own private bar? What about private static fields? That would be very strange, right?

A little long, perhaps, but I hope you can appreciate that after the first shock it really makes sense! (-:

+3
source

Yes, private members in Java are different from some other languages. For example, I do not think that equivalent code in C ++ will be valid.

On the other hand, this will be true in C #. There are still some differences between C # and Java regarding access to private members from nested or nested classes, but they are basically similar.

You should not expect any two languages ​​to have the same behavior. It is worth exploring the specification of the language you are interested in - in this case - section 6.6 of the JLS .

+10
source

In Java, scopes are based on classes, not objects. If something is private, it means that it is visible to the code defined in this class, even if it is an instance field, and the code is executed in another instance.

Perhaps you can think of objects of the same class as the twins who share everything with each other, but who will keep most of the things hidden from everyone else.

+1
source

I could just pass an object of some type for comparison.

You have not done so. You passed an object of type Foo to compare To (). Java rules allow access to any member of any Foo inside the Foo class. C ++ is more limiting that you can only access private members of your own Foo.

+1
source

All Articles