Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Private Field Visibility

So I was making a class the other day and used Eclipse's method to create the equals method when I realized that it generated the following working code:

class Test {
  private int privateInt;
  [...]
  public boolean equals(Object obj) {
    [...]
    Test t = (Test) obj;
    if ( t.privateInt == privateInt ) {
    [...]
  }
}

t.privateInt..???? It's suppose to be private! So I guess there is one more field visibility other than private, protected, package protected and public.

So what is happening here? How is this called? Where would somebody use this? Doesn't this break encapsulation? What if the class didn't have a mutator and I changed this? Does this happen to C++ as well? Is this an OO idiom? If not, then why did Java do it?

Generally, where can I find information about this?

Thank you.

like image 976
pek Avatar asked Nov 23 '08 05:11

pek


People also ask

Can you access private fields in Java?

If we want to access Private Field and method using Reflection we just need to call setAccessible(true) on the field or method object which you want to access. Class. getDeclaredField(String fieldName) or Class. getDeclaredFields() can be used to get private fields.

What is meant by private visibility of a method in Java?

Private in Java means the variable or method is only accessible within the class where it is declared. If your impression about private was true, it will not be accessible anywhere ever which makes it completely useless.

What is meant by private visibility?

A member method of a class declared with private access specifier is said to have private visibility. Only other member methods of its class can call this method.

Can private fields be accessed outside of the class?

Private Access Modifier - PrivateVariables that are declared private can be accessed outside the class, if public getter methods are present in the class. Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world.


1 Answers

It's accessible from different instances of the same class.

According to this page (bolding mine):

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private and protected. The private modifier specifies that the member can only be accessed in its own class.

For clarity I'll rewrite this line:

if ( t.privateInt == this.privateInt )

We can agree that "this.privateInt" should be allowed: you are accessing it from within the instance of class Test that the message "equals" has been sent to.

It's less clear that "t.privateInt" should be visible, because t is a separate instance of class Test and we are not executing inside its equals method. However java allows this since both objects (t and this) are of the same class Test and can see each others private members.

like image 162
Michael Sharek Avatar answered Sep 26 '22 23:09

Michael Sharek