File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Nested Classes in Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Nested Classes in Interfaces" Watch "Nested Classes in Interfaces" New topic
Author

Nested Classes in Interfaces

Sarah Rising
Ranch Hand

Joined: May 06, 2011
Posts: 38
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?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

i'm not sure of my answer. as far as i know interfaces are implicitly static. so NCNI would be static.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sarah Rising
Ranch Hand

Joined: May 06, 2011
Posts: 38
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)?
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
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,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Sarah Rising
Ranch Hand

Joined: May 06, 2011
Posts: 38
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

Joined: Oct 13, 2009
Posts: 774
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

Joined: May 06, 2011
Posts: 38
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
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

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

Joined: May 06, 2011
Posts: 38
Thanks very much, Henry. Apparently, I'm having a couple of bad days these last few.
Sarah Rising
Ranch Hand

Joined: May 06, 2011
Posts: 38
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

Joined: Oct 13, 2009
Posts: 774
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

Joined: Oct 13, 2009
Posts: 774
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,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nested Classes in Interfaces