简体   繁体   English

静态嵌套类可以多次实例化吗?

[英]Can a Static Nested Class be Instantiated Multiple Times?

Given what I know of every other type of static feature of programming––I would think the answer is 'no'. 鉴于我所知道的其他类型的静态编程功能 - 我认为答案是'不'。 However, seeing statements like OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass(); 但是,看到像OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();这样的语句OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass(); makes me wonder. 我好疑惑。

Yes, there is nothing in the semantics of a static nested type that would stop you from doing that. 是的, static嵌套类型的语义中没有任何内容可以阻止您这样做。 This snippet runs fine. 这个片段运行正常。

public class MultipleNested {
    static class Nested {
    }
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Nested();
        }
    }
}

See also 也可以看看


Now, of course the nested type can do its own instance control (eg private constructors, singleton pattern, etc) but that has nothing to do with the fact that it's a nested type. 现在,嵌套类型当然可以进行自己的实例控制(例如private构造函数,单例模式等),但这与嵌套类型无关。 Also, if the nested type is a static enum , of course you can't instantiate it at all. 此外,如果嵌套类型是static enum ,当然您根本无法实例化它。

But in general, yes, a static nested type can be instantiated multiple times. 但总的来说,是的, static嵌套类型可以多次实例化。

Note that technically, a static nested type is not an "inner" type. 请注意,从技术上讲, static嵌套类型不是“内部”类型。

JLS 8.1.3 Inner Classes and Enclosing Instances JLS 8.1.3内部类和封闭实例

An inner class is a nested class that is not explicitly or implicitly declared static . 内部类是未显式或隐式声明为static的嵌套类。

That is, according to JLS terminology, an inner class is one that isn't static . 也就是说,根据JLS术语, 内部类是非static If it's static , then it's just a nested type. 如果它是static ,那么它只是一个嵌套类型。


So what does static mean? 那么static意味着什么呢?

static simply means that the nested type does not need an instance of the enclosing type to be instantiated. static只是意味着嵌套类型不需要实例化封闭类型的实例。

See also 也可以看看

@polygenelubricants : But in general, yes, a static nested type can be instantiated multiple times. @polygenelubricants:但总的来说,是的,静态嵌套类型可以多次实例化。

Just to be sure 100% of that I extended your snippet: 只是为了确保100%我扩展你的片段:

public class MultipleInner {
    static class Inner {
        private int state;
        public int getState() { return state; }
        public void setState(int state) { this.state = state; }
    }

    public static void main(String[] args) {
        List<Inner> inners = new ArrayList<Inner>();
        for (int i = 0; i < 100; i++) {
            Inner inner = new Inner();
            inner.setState(i);
            inners.add(inner);
        }
        for (Inner inner : inners) {
            System.out.println(inner.getState());
        }
    }
}

And of course the result is: 当然结果是:

0
1
2
3
.
.
.
97
98
99

It is legal. 这是合法的。 The fact that the inner class is static gives you a benefit here; 内部类是静态的这一事实在这里给你一个好处; its instances are not bound to any instance of the containing class, so they can be freely instantiated (as long as the access qualifier allows it). 它的实例没有绑定到包含类的任何实例,因此可以自由地实例化它们(只要访问限定符允许它)。

The price, however, is that the inner class can't use non static members/methods of the containing class. 然而,价格是内部类不能使用包含类的非静态成员/方法。

Inner class can use non static members/methods of containing class. 内部类可以使用非静态成员/包含类的方法。 It can use them only through an object reference of the enclosing class- 它只能通过封闭类的对象引用来使用它们 -

     public class MultipleInner {
      private int outerstate =10;
      static class Inner {
        private int state;
        public int getState() { return state; }
        public void setState(int state) { this.state = state; }
      }

     public static void main(String[] args) {       
        Inner inner = new Inner();
        inner.setState(new MultipleInner().outerstate);
        System.out.println(inner.getState());        
     }

} }

So, inner class doesn't have to pay the price of not being able to access the non static members of the enclosing class. 因此,内部类不必支付无法访问封闭类的非静态成员的代价。

Yeah you can make instances of it as many times as you want. 是的,你可以根据需要多次制作它。

Maybe the reason why you see that, is because the programme thought about storing a reference somewhere. 也许你之所以看到这个,是因为该程序考虑在某处存储引用。 Though i agree with you seems strange :S 虽然我同意你的看法很奇怪:S

Static nested classes are indeed instanced - they are, as said, top-level classes which live in the namespace of the 'outer' class, and obey static semantics respecting references to the 'outer' class. 静态嵌套类确实是实例化的 - 如上所述,它们是位于“外部”类的命名空间中的顶级类,并遵循关于“外部”类的引用的静态语义。 This code sample demonstrates : 此代码示例演示:

public class OuterClass {
    String outerStr = "this is the outer class!!" ;
    public static class StaticNestedClass {
        String innerStr = "default / first instance" ;      
    }

    public static void main(String[] args) {
        OuterClass.StaticNestedClass nestedObject1 = new OuterClass.StaticNestedClass();        
        OuterClass.StaticNestedClass nestedObject2 = new OuterClass.StaticNestedClass();
        nestedObject2.innerStr = "second instance" ;
        System.out.println(nestedObject1.innerStr) ;
        System.out.println(nestedObject2.innerStr) ;
    }
}

output:

default / first instance 
second instance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM