• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner classes in interfaces (how long has this been around?)

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading my Exam Cram book and I had it drilled into my head that Interfaces only allow constants and abstract methods.

For some reason though I wondered if I could throw an inner class in there.
And to my surprise it could. To my even greater surprise the inner class can actually be non abstract, can contain code etc.

How long has this been a feature of Java? I'm studying from the Exam Cram for 1.4 so I'm guessing this is a 1.5 feature? Pretty neat but I'm not sure about the rational behind it.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I'm not mistaken, this has been in place since 1.1 or 1.2.

Note the Preface to the Second Edition of the JLS...

This second edition of The Java Language Specification ... integrates all the changes made to the Java programming language since the publication of the first edition in 1996. The bulk of these changes were made in the 1.1 release of the Java platform in 1997, and revolve around the addition of nested type declarations.


In particular, Chapter 8 of the 2nd edition says, "A nested class is any class whose declaration occurs within the body of another class or interface."
 
Panseer Kaur
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm this really makes me doubt the usefulness of the Exam Cram books.

I guess this is a way you can essentially have multiple inheritance though. You could essentially create a car object but then just implement interfaces with inner classes so you can have a motor object, a tire object and everything else without having to pile all that code into any one class.

Interesting.
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interfaces can't have inner classes, but they can contain static nested classes. All interface members are static by default, so member classes are static even if you don't explicitly specify the modifier.

It's definitely very poor form to write interfaces with huge full-fledged classes, even though the language allows it (rather unfortunately, in my opinion). In the past, some of the main uses of member classes in interfaces was for grouping static final constants and for emulating enum types. (Java 1.5's built-in enum types have since superseded the latter use.)
 
Panseer Kaur
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Interfaces can't have inner classes, but they can contain static nested classes. All interface members are static by default, so member classes are static even if you don't explicitly specify the modifier.



I'm really confused by that. Does that mean that you cannot create a instance of a interface member class? Because I tried to access the interface inner class without an instance but it would not work.

However this code without having the interface inner class as static did work:

 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are getting confused on terminology.

An inner class is a non-static nested class.

However, if you have a class definition in an interface, it is implicitly public and static.

So an interface can't have an "inner" class.

A class definition in an interface is automatically a static nested class.
 
Panseer Kaur
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the example I posted what would the class inside the interface be called? I know you said

A class definition in an interface is automatically a static nested class.



But the class I instantiated came from a interface. I would assume that means it was not static at all.

Thanks
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Panseer Kaur:
In the example I posted what would the class inside the interface be called? ... But the class I instantiated came from a interface. I would assume that means it was not static at all...


In your example, x is simply a "nested class." It is not an "inner class" because it is implicitly static.

The code demonstrates that x is static, because the instance of iii.x was created without ever instantiating Child (which is also an iii).
 
Panseer Kaur
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it thanks.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic