Can a static nested class be instantiated in Java?

2019-03-09 02:26发布

问题:

From Oracle's Java tutorials I've found this text:

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class — it can use them only through an object reference.

Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

Static nested classes are accessed using the enclosing class name:

OuterClass.StaticNestedClass

For example, to create an object for the static nested class, use this syntax:

OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

I thought it is not possible to instantiate a static class, so I don't really understand the sentence in bold.

Do you have any idea what it means?

回答1:

You are either confusing static with abstract as kihero says, or you are muddling the concept with a class that has static methods (which is just a class that happens to have static methods).

A static nested class is just a nested class that doesn't require an instance of its enclosing class. If you are familiar with C++, all classes in C++ are "static" classes. In Java, nested classes are not static by default (this non-static variety is also called an "inner class"), which means they require an instance of their outer class, which they track behind the scenes in a hidden field -- but this lets inner classes refer to fields of their associated enclosing class.

public class Outer {

    public class Inner { }

    public static class StaticNested { }

    public void method () {
        // non-static methods can instantiate static and non-static nested classes
        Inner i = new Inner(); // 'this' is the implied enclosing instance
        StaticNested s = new StaticNested();
    }

    public static void staticMethod () {
        Inner i = new Inner(); // <-- ERROR! there's no enclosing instance, so cant do this
        StaticNested s = new StaticNested(); // ok: no enclosing instance needed

        // but we can create an Inner if we have an Outer: 
        Outer o = new Outer();
        Inner oi = o.new Inner(); // ok: 'o' is the enclosing instance
    }

}

Lots of other examples at How to instantiate non static inner class within a static method

I actually declare all nested classes static by default unless I specifically need access to the enclosing class's fields.



回答2:

Static nested classes are themselves not static at all. In java, no class is static. Static keyword in static nested classes implies that it is another static member of the outer class. But it is just another raw class . Thats why we can instantiate this class



回答3:

You are confusing static with abstract. Abstract classes can not be instantiated. static is not a valid qualifier for top level classes, but the meaning for inner classes is the one you quoted.



回答4:

I guess you misunderstood the static class a little bit. It's true that every abstract class and interface cannot be instantiated, but you do can instantiate an static class. One thing you should notice is that every static class is a nested static class. You cannot just create a static class, as you can see: try to create a new class in eclipse

A static class always belongs to the "parent class" which encloses it, and the difference between static and non-static class is: You can refer to the child static class just like a static property of the "parent class":

ParentClass.NestedStaticClass nestedstatic = new ParentClass.NestedStaticClass();

but you can only make reference to the non-static nested class by instantiating a parent class, like this:

ParentClass parent = new ParentClass();
ParentClass.NestedClass nested = parent.new NestedClass();

The difference is just like that between the static and non-static field.