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.
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 bestatic
, as withabstract
. Different keyword, different behaviour.