0

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.

1
  • 5
    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. Jul 16, 2014 at 15:43

4 Answers 4

4

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.

4

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.


With a later edit and some confusion about static, final and state:

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.

I must point out that final variables have state too. It is just a state that can be assigned once and only once. Consider:

final int foo;
if(bar) {
    foo = 42;
} else {
    foo = 4;
}

Perfectly valid and stateful code on a final variable.

A static inner class that you can't instantiate is a situation that I can't quite find a use case for. Yes, the Java designers used static in yet another way (import, class, block, field, method, and inner class too). Each different use of static is distinct and should be considered to be separate concepts that sometimes interact.

In this case, the static inner class is more akin to a static method than a static class.

0
2

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.

1
  • 1
    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. Jul 16, 2014 at 17:11
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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