Hello,
From
JLS-6.6.2,
A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.
I'm trying to understand what a
responsible code might be: a statement, an expression, or only a class?
To use super to access a hidden protected field, works. Because the expression, i.e. super.
Identifier, used to access the
protected field is responsible for implementation, as specified above.
From JLS-6.6.2.1,
Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C.
In addition, if Id denotes an instance field or instance method, then:
If the access is by a qualified name Q.Id or a method reference expression Q :: Id (§15.13), where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.
So I can access a protected member of an object in a static method in S, given that the reference expression used to access the member is of type S or a subclass of S.
But I fail to see how the code used to access a protected non-static member of an object in a static method is responsible for the implementation of the object!
Basically, how do §6.6.2 and §6.6.2.1 not contradict each other?
Thanks.