Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

What's all the static, man?

The difference between inner member classes and inner static member classes

  • Print
  • Feedback

In this Q&A I'll cover two popular and related questions: "What is the difference between an inner member class and an inner static member class?" And: "Why would I choose one over the other?"

Superficially, static and nonstatic inner member classes differ in how you declare them. A static member class will have the static keyword in its definition, while a member class will not:

public class InnerClassSyntax {
    // A static member class
    public static class StaticMember {
        // ... code
    }
    
    // A member class
    public class Member {
        // ... code
    }
}


More importantly, the static keyword limits what instances of the static member classes can do to instances of the class within which they are defined. To wit, when you declare a member class as static, instances of that inner class will have access only to the static methods and static members of the enclosing instance.

In contrast, plain old member classes can access any method or member of the enclosing class since member classes have access to the enclosing instance's this. (A static member class is limited to accessing only static attributes because it lacks access to the enclosing instance's this.)

Let's expand the original example to see these limitations in action:

public class InnerClassSyntax {
    private static int _aStaticInstanceVariable;
    private        int _anInstanceVariable;
    
    public void anInstanceMethod() {
        // ... do something
    }
    
    public static void aStaticMethod() {
        // ... do something
    }
    
    // A static member class
    public static class StaticMember {
        public void aMethod() {
            // Legal calls
            int staticValue = InnerClassSyntax._aStaticInstanceVariable;
            InnerClassSyntax.aStaticMethod();
            
            // Illegal calls -- will not compile if uncommented
            // int value = _anInstanceVariable;
            // anInstanceMethod();
            // InnerClassSyntax.anInstanceMethod();
        }
    }
    
    // A member class
    public class Member {
        public void aMethod() {
            // Legal calls
            int staticValue = _aStaticInstanceVariable;
            int value       = _anInstanceVariable;
            aStaticMethod();
            anInstanceMethod();
        }
    }
    
}


You see that the static member class can access only those attributes declared as static. Static member classes do not have access to the this reference -- in this case InnerClassSyntax.this. However, Member instances do have access to InnerClassSyntax.this, so they can access everything in the enclosing InnerClassSyntax instance.

The following code selection rewrites the Member class so that it explicitly uses the InnerClassSyntax.this reference (which normally happens by default):

    public class Member {
        public void aMethod() {
            staticValue = InnerClassSyntax._aStaticInstanceVariable;
            value = InnerClassSyntax.this._anInstanceVariable;
            InnerClassSyntax.aStaticMethod();
            InnerClassSyntax.this.anInstanceMethod();
        }


Now, with all the syntactical goodies aside, why would you choose a static member class over a plain-old member class? Well, when designing an inner class, you need to ask what do that inner class's instances need? Ask yourself whether the instances depend on instance-specific information? If yes, you need a member class.

  • Print
  • Feedback
What is Tech Briefcase?
TechBriefcase is a new, free service where IT Professionals can Search, Store and Share IT white papers and content like this. Learn more
Bookmark content
Speed up your research efforts with content across the web.
Search and Store
Find the white papers you need. Create folders for any topic.
View Anywhere
Open your briefcase on your iPhone, tablet or desktop. Share with colleagues.
Don't have an account yet?

Resources