What will be the output of the program?

public class Test {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) { 
              i = doIt(i); 
        }
    }
    static int doIt(int i) {
        new Test() {
            {  System.out.print((i + 0.99) + ", ");  }
        };
        return i;
    }
}
Explanation
A variable used inside an anonymous class object defined outside the anonymous class, had to be declared as final before Java 8.
Starting in Java SE 8, a local class can access local variables and parameters of the enclosing block that are final or effectively final. A variable or parameter whose value is never changed after it is initialized is effectively final.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback