Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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);
        }
    }
}