File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can you have an inner class in an interface?

 
Marco Farrugia
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Marco Farrugia
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your explanation. That was helpful!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1951
7
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic