PrivateFromStatic

Misconception:

Private members of an object cannot be accessed by static methods, because static methods are not specific to an object.

Incorrect

Static methods cannot access private members of instances of same class

Correct

Static methods can access private members of instances of same class

Correction
Here is what's right.

Access permissions are per class, not per object.

class C {

    private int instanceField;

    private void instanceMethod() {
    }

    public static void m() {
        C o = new C();
        o.instanceField++;  // ok
        o.instanceMethod(); // ok
    }
}

Any static method of a class can access private instance members (e.g., fields, methods) of any instance of that same class.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.