• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Inner classes and interfaces?

 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a principle that says the "non-static inner classes cannot have static members". And there is another principle that says "interfaces are static by default".

That made me wonder if it was possible that an inner non-static class have an interface declaration. Strangely the code you see below compiled in JDeveloper 10g (Preview Edition), but not in the command line... at the end the JDeveloper compiler threw a compiler internal error.

But, what can I learn from all this? Is it, or is it not possible to declare an interface in a non-static inner class?
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interfaces are NOT static by default.
ONLY data members in interfaces are static, methods declared in interfaces are NEVER static.

So you can declare an inner class to implement any interface that doesn't have data members.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


interfaces are static by default



Your experiment is on a false premise.
Would you care to share with us where you got this idea from?
Perhaps that interface members that are not methods (fields, and inner classes) are implicitly static?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeroen Wenting:
interfaces are NOT static by default.


Edwin means that memeber interfaces are implicitly static and I think this is right.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no, an interface is never static.
An interface may contain static members, but only if those members are data members and not methods.

If an interface were implicitly static you could not implement their methods in classes implementing their methods unless those methods were static...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bilal is correct. From JLS2 8.5.2: "Member interfaces are always implicitly static. It is permitted but not required for the declaration of a member interface to explicitly list the static modifier." We're not saying that the methods in the interface are static (which is false), or that fields in an interface are static (which is true). We're saying that the nested interface itself is static. Static in the sense of "static nested class" (or interface, rather).

[Edwin]: There is a principle that says the "non-static inner classes cannot have static members".

Inner classes cannot declare static members (where members includes methods, fields, and nested types). They can however inherit static members from a superclass (or superinterface).

And there is another principle that says "interfaces are static by default".

Nested interfaces are implicitly static. It's not just the default for nested interfaces - they're static, period, and we can't possibly change that.

That made me wonder if it was possible that an inner non-static class have an interface declaration. Strangely the code you see below compiled in JDeveloper 10g (Preview Edition), but not in the command line... at the end the JDeveloper compiler threw a compiler internal error.

But, what can I learn from all this? Is it, or is it not possible to declare an interface in a non-static inner class?


No, it isn't. Sounds like this was a bug in JDeveloper.
[ January 30, 2005: Message edited by: Jim Yingst ]
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ah of course, you're talking about interfaces declared as members of a class, not members declared in an interface
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic