| Author |
Inner Classes
|
nishant kumarmca
Greenhorn
Joined: Apr 03, 2010
Posts: 16
|
|
|
Can we write inner class inside an interface? If yes then what does it means?
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
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
Joined: Apr 03, 2010
Posts: 16
|
|
|
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
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
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
Joined: Apr 03, 2010
Posts: 16
|
|
|
can you please explain me in which scenario we should use inner classes inside the interface?
|
 |
Anbarasu Aladiyan
Ranch Hand
Joined: Jun 02, 2009
Posts: 182
|
|
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)
|
A.A.Anbarasu
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2782
|
|
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
Joined: Mar 17, 2010
Posts: 1046
|
|
|
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
|
SCJP6.0,My blog Ranchers from Delhi
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
A slightly different slant:
Classes that implement this interface have direct access to UTIL - thus
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
Shanky : Thank you for a clear and useful example.
Jim ... ...
|
BEE MBA PMP SCJP-6
|
 |
 |
|
|
subject: Inner Classes
|
|
|