简体   繁体   English

Java最佳实践-内部类访问外部类的静态变量

[英]java best practice -outer class static variable accessed by inner class

class OuterClass {
    static int j=99;

    static class InnerClass {

        public void doSomething() {
            while(true)
                OuterClass.j= 100;
            } 
        }
    } 
}   

Using the above code, is there any issue with garbage collection when the OuterClass needs to garbage collect but lets say doSomething is referencing a static variable will it be an issue ? 使用上面的代码,当OuterClass需要进行垃圾收集时,垃圾收集是否有任何问题,但是可以说doSomething引用了一个静态变量,这会成为问题吗? What i'm concerned with is that the inner class has a reference to the outer class by accessing the variable j and im wondering if that will affect the OuterClass being GC. 我关心的是,内部类通过访问变量j来引用外部类,我想知道这是否会影响作为GC的OuterClass。

Although you've named it InnerClass , it is not an inner class. 尽管您已将其命名为InnerClass ,但它不是内部类。 It is simply a static nested class. 它只是一个static嵌套类。

An instance of such a class has no reference to any instance of the enclosing class. 此类的实例没有引用任何封闭类的实例。 What's more, you're referencing a static field of the enclosing class. 此外,您引用的是封闭类的static字段。 There is absolutely no reference to any instance of OuterClass . 绝对没有引用OuterClass任何实例。

Nothing in your InnerClass can prevent an instance of the type OuterClass from being garbage collected. 在你没有InnerClass可以防止类型的实例OuterClass被垃圾收集。

Did I misunderstand your question? 我误会了你的问题吗? Are you asking about classes being garbage collected? 您是在问类被垃圾回收吗?

Classes aren't affected by GC, objects are. 类不受GC的影响,对象受GC的影响。 If you create an object of type InnerClass, GC will not touch anything referred to by it. 如果创建类型为InnerClass的对象,GC将不会触摸其引用的任何对象。 However, because you are only referencing a static variable, you are not referencing any objects of type OuterClass (in that code fragment anyway). 但是,由于仅引用静态变量,因此没有引用OuterClass类型的任何对象(无论如何在该代码片段中)。 Thus, GC is free to remove any unneeded objects of type OuterClass without affecting, in any way, the functioning of objects of type InnerClass. 因此,GC可以自由删除任何OuterClass类型的不需要的对象,而不会以任何方式影响InnerClass类型的对象的功能。

Note that if all you're doing is referring to a static variable in OuterClass from your InnerClass, you should probably not define InnerClass as a nested class. 请注意,如果您所做的只是从InnerClass引用OuterClass中的静态变量,则可能不应将InnerClass定义为嵌套类。

Since the j field is static, it exists beyond the scope of a single instance of OuterClass. 由于j字段是静态的,因此它存在于OuterClass单个实例的范围之外。 The fact that InnerClass is static decouples it from any instance of OuterClass. InnerClass是静态的这一事实使它与OuterClass的任何实例分离。

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. 实际上,静态嵌套类在行为上是顶级类,为了包装方便,该顶级类已嵌套在另一个顶级类中。

Nested classes explained in the Java tutorial Java教程中解释的嵌套类

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

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