Results 1 to 6 of 6
- 12-12-2011, 10:30 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 30
- Rep Power
- 0
Understanding 'static' a little more
Hi everyone,
I have a class called Cache, which has an inner class called CacheMember, representing the members in the cache. I made the inner class CacheMember a static inner class, and I don’t really understand that part. (It compiled, and so far I haven’t seen any undesirable effects coming from that, but I don’t understand why it had to be static.) From my understanding, static means that there’s only one instance of the class called, but I can call as many CacheMembers as I want...
- 12-12-2011, 11:11 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Understanding 'static' a little more
A bit of jargon from the JLS 8.1.3 Inner Classes and Enclosing Instances: "An inner class is a nested class that is not explicitly or implicitly declared static." So your CacheMember would be described as "static nested" rather than "inner".
Of course the name is not what really matters! I think Roedy Green's Glossary has a good discussion about what it means for a nested class to be static.
From that page (although the whole thing is worth a read): "There are two flavours of nested classes, static classes (which can be allocated independently of any particular outer class object), and inner classes which are always allocated in conjunction with some particular instance of the outer class object." The important thing is that the outer class (Cache) provides the context for the inner class (CacheMember).
If an instance of CacheMember can get along fine without having access to the state of any particular Cache instance then it is static. If not then it's inner.
The Collection framework provides examples of both sorts of nested class. (You can get at the code in src.zip in the jdk installation directory.) Key/value pairs in a map (java/util/AbstractMap.java) are represented by a static SimpleEntry<K,V> class because entries are quite "unaware" of the map of which they are an entry - or even if there is such a map. On the other hand the iterator returned by a list (java/util/AbstractList.java) is actually an instance of a nested class Itr which cannot be static because an iterator has to be "aware" of the collection it is iterating over. By being a so-called inner instance class Itr has all sorts of access to the map and - as seems to commonly be the case - it is made private.
- 12-12-2011, 11:18 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Understanding 'static' a little more
To address this directly, you can have as many instances of a static nested class as you like. What makes the class static is that the instances don't have as their context any particular Cache instance.From my understanding, static means that there’s only one instance of the class called, but I can call as many CacheMembers as I want...
- 12-12-2011, 11:25 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 30
- Rep Power
- 0
Re: Understanding 'static' a little more
Got it, Brock. Thanks for the quick response!
- 12-12-2011, 11:26 PM #5
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: Understanding 'static' a little more
That is not what static means here (or in general).
static, here, is
a) for organizational purposes -- often a good place to put a small class which is closely related to a bigger class.
b) for access purposes. A static nested class can access the private static members of its scoping class because it is within scope. It exists independently of any instance of its scoping class, so it cannot access non-static members.
- 12-12-2011, 11:37 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Similar Threads
-
Error in Code: Non-static method cannot be referenced from a static context
By oaklandsbest in forum New To JavaReplies: 9Last Post: 06-10-2011, 12:40 AM -
Can't make static reference to non-static method -> huh?! Simple car prgm
By enerj in forum New To JavaReplies: 7Last Post: 09-24-2010, 05:09 AM -
non-static method getType cannot be referenced from a static contex
By Dekkon0 in forum New To JavaReplies: 4Last Post: 05-12-2010, 11:05 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks