Static nested class instance

时间:2015-10-30 22:07:09

标签: java static nested nested-class

I came across this in java, I know its a static nested class but why create an instance of 'static'. Isn't the whole idea of 'static' to use it without an instance? I understand the concept of inner classes, but why (and frankly how is it possible to) create an 'instance' of a 'static' member at all ?

Why this:

1- OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
2- nestedObject.aNestedClassMethod();

and not this:

1- OuterClass outerInstance=new OuterClass();
2- outerInstance.StaticNestedClass.aNestedClassMethod();

3 个答案:

答案 0 :(得分:1)

Use on inner classes, the keyword static indicates that you can access the inner class without an instance of the outer class.

For example:

public class Outer {
    public static class Inner {
        /* Code here. */
    }
}

with this construction, you can create an instance of the inner class by using this code:

new Outer.Inner()

If the inner class would not be static, but also like this:

public class Outer {
    public class Inner {
        /* Code here. */
    }
}

Then, you would have to create an instance of Outer in order to access Inner:

new Outer().new Inner()

答案 1 :(得分:0)

Don't think of a static class as being a member of its enclosing class. It's a class of its own, totally separate. The only real distinction between it and a top-level class is that it has a slightly different name, and it can be private — which again doesn't affect its semantics, other than the fact that only the enclosing class knows about it.

So, if I have:

public class EnclosingClass {
    public static class InnerClass {
    }
}

Then anyone can come around and do:

EnclosingClass.InnerClass instance = new EnclosingClass.InnerClass();

See: exactly the same as a top-level class.

This is actually true of an inner class, too. There things are slightly more complicated, but basically the idea is that the inner class is still its own class, but it has a (mostly hidden) reference to the instance of the enclosing class that created it. I say "mostly" because it's possible to access that instance, via EnclosingClass.this. The java compiler also does some convenience plumbing for you, such that someFieldInTheEnclosingClass becomes EnclosingClass.this.someFieldInTheEnclosingClass. But don't let that shorthand fool you: the inner class is its own separate class, and its instance is its own separate instance; they're no different than a top-level class in that regard.

答案 2 :(得分:0)

Thanks everyone who replied and commented. I have finally got the hang of it. I guess the answer was right in front of my eyes. The reason behind calling a an inner static class 'static' is how its referenced and not how the class behaves. Since we don't need and instance of the OuterClass to create an instance of InnerClass and we simply use OuterClass name, rather than its object to instantiate the inner class. Special thanks to Sleiman Jneidi, who posted the very first comment, the name static here "is" misleading. 1- OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();