简体   繁体   English

我可以从静态内部类访问外部类的字段吗?

[英]Can I access an outer class's field from a static inner class?

I have a class which has another static inner class:我有一个类,它有另一个static内部类:

class A {
    private List<String> list;

    public static class B {
        // I want to update list here without making list as static 
        // I don't have an object for outer class
    }
}

You generally use static classes when you don't need access to the instance variables.您通常使用静态类时,你不需要访问实例变量。 If you need to access the instance variables make the class non-static.如果您需要访问实例变量,请使类成为非静态的。

As you can see from other answers that you will need a non-static inner class to do that.正如您从其他答案中看到的那样,您将需要一个非静态内部类来做到这一点。

If you really cannot make your inner class non-static then you can add required getter and setter method in outer class and access them by creating an instance of outer class from inside inner static class:如果你真的不能让你的内部类非静态,那么你可以在外部类中添加所需的 getter 和 setter 方法,并通过从内部静态类内部创建外部类的实例来访问它们:

public class A {
    private List<String> list = new ArrayList<String>();
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public static class B {
        // i want to update list here without making list as static
        void updList() {
            A a = new A();
            a.setList(someOtherList);
            System.out.println(a.getList());
        }
    }
} 

No, you'll need a non- static inner class to do that.不,你需要一个非static内部类来做到这一点。

From the JLS §8.5.1 :JLS §8.5.1

The static keyword may modify the declaration of a member type C within the body of a non-inner class or interface T . static关键字可以在非内部类或接口T的主体内修改成员类型C的声明。 Its effect is to declare that C is not an inner class.它的作用是声明C不是内部类。 Just as a static method of T has no current instance of T in its body, C also has no current instance of T , nor does it have any lexically enclosing instances.正如T的静态方法在其主体中没有T当前实例一样, C也没有T当前实例,也没有任何词法封闭实例。

It is a compile-time error if a static class contains a usage of a non-static member of an enclosing class.如果static类包含对封闭类的非静态成员的使用,则会出现编译时错误。

In your code, list is an instance variable of class A and B is nested static class.在您的代码中, listA类的实例变量, B是嵌套静态类。 The rules of accessing static and not static member don't change for nested static class.对于嵌套的静态类,访问静态和非静态成员的规则不会改变。

  • The variable list is instance variable so a can't be accessed from static context.变量list是实例变量,因此无法从静态上下文访问。
  • To enable this, you need to change static nester class to inner class.要启用此功能,您需要将静态嵌套类更改为内部类。

     class A { private List<String> list = new ArrayList<String>(); public class B { public void someMethod(){ list.add("abc"); } } }

private List<String> list;

Here list is an instance variable while the inner class is static which means that class B is the same across different class A instances;这里的list是一个实例变量,而内部类是静态的,这意味着B在不同的A 类实例中是相同的; one cannot access it.一个无法访问它。 And, that too for a very good and obvious reason.而且,这也是一个非常好的和明显的原因。

One solution that you can use is pass reference of member list to the constructor of class B with a weakReference to avoid memory leak.您可以使用的一种解决方案是使用引用将成员列表的引用传递给类 B的构造函数以避免内存泄漏。

Something like this:像这样的东西:

class A {
    private List<String> list;

    public static class B {
        WeakReference<List<String>> innerList;
        //constructor
        B(WeakReference<List<String>> innerList){
            this.innerList = innerList;
        }
    }

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

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