This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
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: 4344
    
    8

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.
 
Consider Paul's rocket mass heater.
 
subject: private inner class and static private inner class
 
Similar Threads
Understanding Modifiers for Inner classes
Why not nesting static classes within a method?
Clarification wanted: Extending Threads vs. Implementing Runnable
Why do we need static nested class?
Inner static class as test harness