Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

static inner class nested in an interface  RSS feed

 
Joe Nguyen
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know what is the use of static inner class embeded within an interface?
 
Antoine Waugh
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know what is the use of static inner class embeded within an interface?
well it makes no sense to me. firstly a 'static inner class' is more commenly known as a 'nested class' , and inner classes are generally refered to when there can be multiple instances of an inner class..
interfaces are used to define methods to be inherited/used. each subclass will then have different instances defined. however, as stated before you will only ever have one 'copy' if you like- of that static class..
is there a code example of such an attempt or was it a hypothetical question?
twans
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually Map.Entry from the java.util package would be a good example. Hope it helps!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Almost, but that's a static nested interface defined within another interface. It's a bit more unusual to have a static nested class inside an interface - but it's possible. I don't know of any examples in the standard APIs. However one possible application that comes to mind is if we're designing an interface with methods that throw some custom exception that's only used by classes that implement that interface. E.g.

This allows you to define the interface and the exception it uses in the same place, which might be convenient.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!