To modify a variable in an inner class, you can either pass the instance of the outer class to the inner class and modify the variable using that instance, or declare the variable as "final" and modify it using a setter method within the outer class. Here is an example:
public class OuterClass {
private int variable = 10;
public class InnerClass {
public void modifyVariable() {
// method 1:
OuterClass.this.variable = 20;
// method 2:
// declare the variable as final in the outer class
// and create a setter method
// OuterClass.this.setVariable(20);
}
}
}
Asked: 2022-04-04 11:00:00 +0000
Seen: 11 times
Last updated: May 14 '22