Results 1 to 6 of 6
  1. #1
    salmontres is offline Member
    Join Date
    Dec 2011
    Posts
    30
    Rep Power
    0

    Default 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...

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default 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.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Understanding 'static' a little more

    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...
    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.

  4. #4
    salmontres is offline Member
    Join Date
    Dec 2011
    Posts
    30
    Rep Power
    0

    Default Re: Understanding 'static' a little more

    Got it, Brock. Thanks for the quick response!

  5. #5
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Understanding 'static' a little more

    Quote Originally Posted by salmontres View Post
    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...
    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.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

Similar Threads

  1. Replies: 9
    Last Post: 06-10-2011, 01:40 AM
  2. Replies: 7
    Last Post: 09-24-2010, 06:09 AM
  3. Replies: 4
    Last Post: 05-12-2010, 12:05 PM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •