Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I was playing around with Java today and I read about static inner classes. Why can you have 'statefulness' inside of a static inner class. For instance:

class outerClass { 
        static class Test { 
        private String a; 

        String getA() { return a; } 

        void setA( String newA) {a = newA; } 

    }
}

Am I misunderstanding something? It seems like you should not be able to keep mutable state inside of a class that is labled at static. Moreover it seems like you should not be able to instantiate something that is a static class, it should be a static singleton. Perhaps someone could correct me if I am making an incorrect assumption or enlighten me to why the Java authors decided to make this possible.

EDIT: I feel as if I am confusing the keyword final and static in java, as final variables do not have state. It still seems very strange to be able to instantiate a static class, though.

share|improve this question
4  
static on a nested class means that the class doesn't need to maintain a pointer to its enclosing outer class. It doesn't mean that all fields and methods must be static, as with abstract. Different keyword, different behaviour. –  Kilian Foth Jul 16 at 15:43

4 Answers 4

up vote 3 down vote accepted

A static inner class is the same as a non-inner class. Your example is equivalent to:

class outerClass {}

class Test {
    ...
}

The static in static inner classes means that instances of the inner class aren't associated with an instance of the outer class. Without static, an instance of Test is always attached to an instance of outerClass - you'd need to construct an instance of outerClass before you can construct an instance of Test and the instance of Test would have access to the members of outerClass.

share|improve this answer

You can keep static state. It is just associated with the class, rather than with the object instance. So every object instance sees the same state value.

share|improve this answer
    
That's not really true for the nested class, is it? In the example above, you can have different instances of Test with different values for a. They just can't access any hypothetical non-static members of outerClass. –  Kilian Foth Jul 16 at 17:11

static things aren't always stateless. Instead, static refers to the ability to access the instance variables and methods in an object.

With nested classes in Java, the static modifier on the inner class refers to its access to the outer class.

A static inner class does not have access to the instance variables of its enclosing class (just like a static method doesn't have access to the instance variables). A further implication of this is that because the static inner class doesn't have the ability to access the instance variables it can be instantiated separately from the outer class. However, there is nothing in this definition that implies the lack of state.

The reason to use a static inner class is to further encapsulate it so that it can't tinker with its outer class member variables or methods.

share|improve this answer

The keyword "static" in Java just means objects of that class are not tied to the enclosing class. It has nothing to do with static methods and fields. A static class is even more normal than a normal Java nested class.

You may be thinking in terms of the C# static class, which does not allow state, and contains only static members. (C# is a bit more consistent with the term.) I have heard people, even when talking about Java, refer to classes with no state as "static", so it pays to check what people mean when they use the term.

If you are familiar with C#, be aware that, deep down, Java and C# are very different languages, and their superficial similarities will trip you up badly if you're not careful.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.