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 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 Android Security Essentials Live Lessons this week in the Android 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: 33
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: 1756
    
    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: 33
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: 1756
    
    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!
 
wood burning stoves
 
subject: Can you have an inner class in an interface?
 
Similar Threads
Code for Method-Local Inner Classes not compiling
about inner classes
Method Local Inner Class Pg 671 Kathy Sierra and Bates SCJP 6
Inner Class
inner class instantiation from outside the the outer class