..\rawcodes\Condo.java:18: error: non-static variable this cannot be referenced from a static context
Arranger inOrder = new Arranger();
Biniman Idugboe wrote:What does the static context think of the inner class?
No. If the class is intended to form part of an object, it is better to have it an instance member. If it is accessible outwith its enclosing type, then maybe it isn't intended to form part of the instance, and it can be marked static. Search for Bruce Eckel's book Thinking in Java; the 3rd edition used to be available free of charge on the net. That has a nice chapter about nested classes in. It also shows that public static nested classes are easy to use from other code. And that non‑private inner classes are awkward to use elsewhere because you have to write new twice.
Biniman Idugboe wrote:. . . Is it not better to declare all inner classes static?
Have you found anything in the JLS (=Java® Language Specification) to support that statement? But once I got rid of the compiler errors in jshell, I managed to get such code to compile:-Let's correct the indentation:-That compiles and runs:
An interface that is nested inside a class, is implicitly static; otherwise the interface will not be accessible outside the enclosing class.
jshell> new Bar().f.foo(999);
How do you know that interface is implicitly static? Have you found anything in the JLS? How do you know there isn't another explanation? If you go through the Map interface and look for Map.Entry you will see it is marked
Question: If we can have implicitly static interface, why can't we have explicitly static interface?
...even though the code doesn't have the keyword static. So the suggestion that nested interfaces are implicitly static is probably correct. It would just be nice to read it in the JLS.
...static interface Map.Entry<K,V> A map entry (key-value pair)....
Stop thinking about static contexts. Think about instance context. Java® is an object language, so you should always think in terms of objects first.
Biniman Idugboe wrote:. . . a possibility that the inner class will be referenced from a static context. . . .
Biniman Idugboe wrote:Thanks for the cow and for the analysis. Here's my understanding of your explanations:
An inner class must be declared static if there is a possibility that the inner class will be referenced from a static context.
Question: Is it not better to declare all inner classes static?
Biniman Idugboe wrote: