aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can you have an inner class in an interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can you have an inner class in an interface?" Watch "Can you have an inner class in an interface?" New topic
Author

Can you have an inner class in an interface?

Marco Farrugia
Ranch Hand

Joined: Jul 20, 2012
Posts: 34
Hi,

Is it possible to have an inner class in an interface and create an instance of it? I tried the code below, but I am getting a compiler error at line 20: qualified new of static class. I had to create the anonymous class due to the obvious reason that an interface cannot be instantiated.

Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

This is a very misleading bit of source code, as a matter of fact
An interface can't actually define an inner class, eventhough it certainly appear to in your code sample.
However what you're code actually defines is this:



The public, static and final modifiers in an interface are implicit. What you have here then is not actuall an inner class, but rather a static nested class.
Eventhough a static nested class is referred to via the enclosing class name, an instance of it is not associated with an instance of the enclosing class, and exists separately.

Try this:


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Marco Farrugia
Ranch Hand

Joined: Jul 20, 2012
Posts: 34
Thanks a lot for your explanation. That was helpful!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Jelle Klap wrote:
The public, static and final modifiers in an interface are implicit.

<edit>do you mean for inner class inside interface?</edit>
public , static are implicit. not final.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

Seetharaman Venkatasamy wrote:
Jelle Klap wrote:
The public, static and final modifiers in an interface are implicit.

<edit>do you mean for inner class inside interface?</edit>
public , static are implicit. not final.


Absolutely correct, of course!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can you have an inner class in an interface?