Can I access an outer class's field from a static inner class?
java
Solution
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:
public class A {
private List<String> list = new ArrayList<>();
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());
}
}
}
Problem
I have a class which has another `static` inner class: ``` 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 } } ```