This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner Classes

 
nishant kumarmca
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we write inner class inside an interface? If yes then what does it means?
 
Paul Clapham
Sheriff
Pie
Posts: 20776
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can. What does it mean? I don't really know. What does it mean to declare an inner class inside a class? Whatever it means there, it means the same thing in an interface.
 
nishant kumarmca
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for replying but how can we instantiate the inner class which is inside the interface? And one more can we also write Method-Local Inner Classes inside the interface?
 
Paul Clapham
Sheriff
Pie
Posts: 20776
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would create an instance of the interface's inner class in exactly the same way you would create an instance of a class's inner class. There is no difference in the syntax.

As for your question about method-local inner classes in an interface, the answer to that should be quite clear if you know what a method declaration looks like in an interface. No?
 
nishant kumarmca
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please explain me in which scenario we should use inner classes inside the interface?
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for bringing up this interesting question.
nishant kumarmca wrote:can you please explain me in which scenario we should use inner classes inside the interface?
After trying it in my system I found that, there is no special in 'declaring inner class inside an interface', because it is same as usual inner classes (classes inside another class)
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except when it isn't.

An important difference is that any class (or interface) declared inside an interface is implicitly static. This means it is not an inner class, by Java's definition of the term - because an inner class is not static. It is a static nested class, or static member class. Key differences are:

Static member class:
- has no reference to any enclosing class instance, because there isn't one
- can declare new static members

Inner (non-static) member class:
- has a reference to the enclosing class instance, if there is one
- cannot declare new static members (though it can inherit them)
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The primary goal of interface is to declare contract between implementation class and its user. It is ok to put constants there even in the form of inner classes but placing your logic to be just inappropriate. If you need namespace use subpackages
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A slightly different slant:





Classes that implement this interface have direct access to UTIL - thus


 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanky : Thank you for a clear and useful example.

Jim ... ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic