Michał Pelc wrote:Could you please verify if I am right here?
As you know, it compiles, runs without exception, and prints "Doing it" for me, as well.
Michał Pelc wrote:I believe part about "non-static" might be misleading
I really struggled to come up with something that explains why Rule #2 makes sense 100% of the time.
With that said, as you may already know, from page 268 your example appears legal because
A static member cannot call an instance member without referencing an
instance of the class. This shouldn’t be a surprise since static doesn’t require any instances
of the class to even exist.
Obviously, we have the required cat instance in your example. It does appear that this rule seems to have a "precedence" over Rule #2. As you know, that section of the book text does not appear to have an example as elaborate as yours. Sorry that I couldn't be more help.
By the way, I once asked a question somewhere ... essentially the answer was something to the effect of (heavily paraphrasing) "It's a book ... you can't take a single sentence and essentially forget the other sentences in the whole book." Anyway, thanks for the question.
I am looking forward to what others have to say on this!
If you go through the JLS (-Java® Language Specification), it tells us under which circumstances we may or may not call an interface method private. It doesn't mention any differences in access in that section; that suggests to me that access is the same as it would be anywhere (JLS link).
JLS§6.6.1 wrote: . . . Otherwise, the member or constructor is declared private, and access is permitted if and only if it occurs within the body of the top level type (§7.6) that encloses the declaration of the member or constructor. . . .
I shall leave you to go through the analogous analogous JLS section about class members to see what it says about access there. What you are doing is not accessing an instance member in a static method,, but passing a reference from which all accessible members can be found. It is analogous to the suggested form of a main() method in Winston's FAQ, or to writing this:-I tried your code on JShell (Java15) and it ran first time:-