• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Nested Classes in Interfaces

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm not sure of my answer. as far as i know interfaces are implicitly static. so NCNI would be static.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



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
 
Sarah Rising
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



But I'm not sure my interpretation of "static" class in an interface is the correct interpretation in this situation.

3) Is that how "static" is to be interpreted as it pertains to a nested class in an nested interface?
4) Does it matter whether the interface is top level (as TLI was in my above examples) or nested (like the interface NInTLC)?
 
Ranch Hand
Posts: 774
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Sarah,

A class declared inside an interface is always public and static. When you don't declare any method in the interface but just an inner
class, in that case an interface just acts as namespace for the qualifying class. You can instantiate and test it as follows


Hope this helps,
 
Sarah Rising
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much, Privthi. I should have thought of that simple test, but I was too invested in the idea it was non-static. Do you have any idea where is it stated that a nested class in an interface is static?
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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,



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?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 is described in section 9.1.4 of the Java Language Specification.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.1.4

The key quote is ...

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).



From the quote, the rules that govern classes and interfaces, which are members of the interface, are in section 9.5 of the JLS.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.5

And the key quote in section 9.5 is ...

A member type declaration in an interface is implicitly static and public. It is permitted to redundantly specify either or both of these modifiers.




Hope this helps,
Henry
 
Sarah Rising
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much, Henry. Apparently, I'm having a couple of bad days these last few.
 
Sarah Rising
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Sarah,

It is mentioned in JLS. Please refer to JLS 6 Specification section 9.6
it is quoted that


Interfaces may contain member type declarations (§8.5). A member type declaration in an interface is implicitly static and public.



A member type declaration can be a enclosed interface or a class. Fields don't come under this category and not even abstract methods
because they are contract.

A small working program

HTH,
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Dear Sarah,
Practical usage is basically all upto the designer. The main advantage high cohesion. Additionally, good namespace cleanup. You can also seen an example of abstract Map
Entry interface.
HTH,
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic