Here is a Java Program that Define an inner class within a for loop
class Outer { int outer_x = 100; void test() { for(int i=0; i<10; i++) { class Inner { void display() { System.out.println("display: outer_x = " + outer_x); } } Inner inner = new Inner(); inner.display(); } } }
Here is a Main class to run above Program
class InnerClassDemo { public static void main(String args[]) { Outer outer = new Outer(); outer.test(); } }
Output of Above Java Program
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100