Sarah Rising wrote:I searched but couldn't find an answer to my confusion on how to view nested classes in (nested or not nested) interfaces. Are such classes considered static or non-static whether or not they are declared such?
For example:
1) Is NCnNI considered non-static (which I take to mean it must have an enclosing class instance (that implements NI) to be instantiated, or is NCnNI static like SCnNI?
Another example:
2) Is NCnNI considered non-static or static?
Henry Wong wrote:...
Which you kinda already answered here ....
> which I take to mean it must have an enclosing class instance (that implements NI) to be instantiated, or is NCnNI static like SCnNI?
at least in terms of creating a test. It should be easy to try it, and see if you actually need an enclosing class or not.
Henry
Prithvi Sehgal wrote:Dear Sarah,
The nested classes inside the interface are always static. Reason is that, inner classes always live under the instance of enclosing class
by instantiating it. We cannot instantiate an interface. That's why.
HTH,
Sarah Rising wrote:
Yes, I understand a) an interface can't be instantiated, and b) now understand a nested class inside an interface is always static. But, I thought there might be another way to think about it. The other way, as I mentioned above, would be to, if a nested class in an interface could be non-static then require than an outer instance of a class that implemented the outer interface TLI and the inner class instance would be associated with that outer instance. I presume that isn't that case, but...
I still can't find where it's mentioned in the JLS that a nested class in an interface is always static. I would think that sort of thing would need to be mentioned in order for it to be clear to compiler writers. Do you know if it's mentioned in the JLS?
The body of an interface may declare members of the interface, that is, fields (§9.3), methods (§9.4), classes (§9.5), and interfaces (§9.5).
A member type declaration in an interface is implicitly static and public. It is permitted to redundantly specify either or both of these modifiers.
Interfaces may contain member type declarations (§8.5). A member type declaration in an interface is implicitly static and public.
Sarah Rising wrote:I suppose I would be remiss if I didn't check to see if anybody knows of where a nested in class in an interface has been used to good effect. I certainly don't know of any good use of the idea.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |