This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Static inner classes ... example? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Static inner classes ... example?" Watch "Static inner classes ... example?" New topic
Author

Static inner classes ... example?

Ian Stone
Greenhorn

Joined: Sep 15, 2004
Posts: 13
Can anyone give an example showing where defining a static
inner class (toplevel nested class) provides assistance ?
I understand the general principle of such a class I am just
drawing blanks currently as to how use of such a construct
might be exploited.

I can see how local anonymous inner classes are useful for
adapter code but static inners I just don't get (yet).

~ Ian
Henrique Sousa
Ranch Hand

Joined: Apr 29, 2004
Posts: 92
There is a Campfire Story that solved that question for me, you might want to read it: getting in touch with your inner class. As for static inner classes, I don't see great use in them. Instance inner classes are good though. Best regards


Henrique Sousa<br />SCJP 1.4<br /> <br />All men die, not all men really live - Braveheart, 1995
Ian Stone
Greenhorn

Joined: Sep 15, 2004
Posts: 13
Thanks Henrique. I remember reading that campfire story alongside
some quite good sections in my mughal Java certification text before.
The campfire story, however, seems to reinforce my initial inclination
that static inner classes have more limited uses than true "instance"
inner classes.

~Ian
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I do not suggest this as a good way to design a system.Note that the static inner class has access to the private static outer class member.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Sun tells us that we're supposed to call them static "nested" classes instead of "inner", and it's a hard habit to break... but I'm just reminding y'all that if they're static, they're considered "nested" and not "inner". (I'm just the messenger )

Some people use static nested classes more for a naming scope, where non-staic nested (inner) classes are generally more useful, and used a lot in the Java API. I can't think of a time I've ever needed to use a static nested class, and I can't recall off the top of my head where I've seen a good example of one in the Java API, whereas inner classes are used in all sorts of things.

cheers,
Kathy
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Here is something I do often.
Suppose you have an interface:


Then your application will have only one implementation, however, if you expose that implementation as a public class, you run the risk of silly clients (clients are never silly!) using a reference of the concrete type. Then when it's time to perform a change (such as a feature or a fix), you have to support that concrete type for reverse compatibility. This is where the Factory Design Pattern becomes handy (there are other times).

Instead expose a factory method that clients use:

*** Your implementation that you are free to change - independant of clients, provided the interface contract is met, which is usually stipulated in the javadoc.

This forces (silly) clients to use the interface type and they never get to see the implementation.

[ edited to move long unbroken comment line to outside of code body -ds ]
[ September 21, 2004: Message edited by: Dirk Schreckmann ]

Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static inner classes ... example?
 
Similar Threads
Accessing inner class from method
Help with Inner Classes
Static Inner Class Question
Difference between inner and outer classes
Nested Classes