It's not a secret anymore!
The moose likes Java in General and the fly likes private inner class and static private inner class 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 » Java in General
Reply Bookmark "private inner class and static private inner class" Watch "private inner class and static private inner class" New topic
Author

private inner class and static private inner class

hegde prasanna
Greenhorn

Joined: Dec 21, 2009
Posts: 4
Hi,
I understand the concept and usage of inner classes in general.
When should we go for a private inner class and when for a static private inner class? I tried searching but it wasn't of much help.
Basically I need to design a caching solution in which I need to timestamp the data object. After timestamping, data will be stored in a HashMap or some other collection. I'm planning to use a wrapper class (which is inner and private) which holds the data object and timestamp. I can make the program work by using either normal inner class or static inner class, however would like to know which is better in such case. Also If I can get some general guidelines as to when to use a staic inner class and when to use a normal inner class, it would help me.

Thanks in advance.



Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3793
    
    1

I'd generally advise making it static unless that doesn't do what you need. So the question is, does it need access to instance members of the containing class? If yes, you've got to make it an inner class. If no, there's no reason not to make it a static class.

This is following the principle that you should make things only as accessible as they need to be - the same reason that I'd make methods private if there's no reason not to. An inner class can access more than a static inner class.
hegde prasanna
Greenhorn

Joined: Dec 21, 2009
Posts: 4
Thanks Matthew. That makes sense to me. I don't need to access anything in the outer class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: private inner class and static private inner class
 
Similar Threads
Why do we need static nested class?
Understanding Modifiers for Inner classes
Clarification wanted: Extending Threads vs. Implementing Runnable
Inner static class as test harness
Why not nesting static classes within a method?