How is this private variable accessible?

A private member is accessible from any method within the class in which it is declared, regardless of whether that method accesses its own (this) instance's private member or some other instance's private member.

This is stated in JLS 6.6.1:

...Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

This feature of Java allows you to write methods that accept an instance of the class as an argument (for example - clone(Object other), compareTo(Object other)) without relying on the class having non private getters for all the private properties that need to be accessed.


Private fields are private to the class as a whole, not just to the object.

Other classes do not know that MyClass has a field called count; however, A MyClass object knows that another MyClass object has the count field.


Accessors are not security! They are encapsulation, to keep others from having to know about the code.

Consider if someone wrote a Quantum Bogo Sort, but disappeared once he quashed the last bug -- understanding the code causes one to be either deleted from the universe or to go mad.

Despite this minor drawback, if properly encapsulated, this should become your prefered sorting algorithm, as all fields and methods except Sort should be private.

You don't know how it works, and you don't want to know how it works, but it works and that's enough. If on the other hand, everything is public, and you have to understand how it does what it does to use it correctly -- that's just too much bother, I'll stick with quicksort.


Though it is the instance of the same class in which testPrivate is written, but shouldn't it through a compiler error at System.out.println(o.count);

No. It will never throw a compilation error.

This is much similar to what a simple getter and setter does or a copy constructor does. Remember we can access private members using this.

public MyClass {
  private String propertyOne;
  private String propertyTwo;

  // cannot access otherObject private members directly
  // so we use getters
  // But MyClass private members are accessible using this.
  public MyClass(OtherClass otherObject) {
      this.propertyOne = otherObject.getPropertyOne();
      this.propertyTwo = otherObject.calculatePropertyTwo();
  }

  public void setPropertyOne(String propertyOne) {
      this.propertyOne = propertyOne;
  }

  public String getPropertyOne() {
      return this.propertyOne;
  }
}

Your testPrivate method accepts an instance of MyClass. Since testPrivate is a method inside MyClass, it will have access to private properties.

  public void testPrivate(MyClass o) {
      this.propertyOne = o.propertOne;
  }

Methods defined inside the class will always have access to it's private members, through this. and instance variable.

But if you define testPrivate outside of MyClass then, you won't have access to private members. There you will have to use a method or a setter or a getter.