• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Can you have an inner class in an interface?

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your explanation. That was helpful!
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic