Can a static nested class be instantiated in Java?

后端 未结 4 1836
滥情空心
滥情空心 2020-12-23 21:25

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. An

相关标签:
4条回答
  • 2020-12-23 21:39

    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.

    0 讨论(0)
  • 2020-12-23 21:40

    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.

    0 讨论(0)
  • 2020-12-23 21:48

    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

    0 讨论(0)
  • 2020-12-23 21:55

    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.

    0 讨论(0)
提交回复
热议问题