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
CorrectionHere is what's right.
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.
Language
Java