I know,this question has been asked already and I an advance apology for asking it again but I am not able to figure out the exact reason why this is so?Can someone please help me out with it? For e.g.-
public class File
{
public static void main(String[] main)
{
File obj=new File();
obj.method();
}
void method()
{
for(int i=0;i<10;i++)
{
class Inner
{
void display()
{
System.out.println("i is : "+i); //Gives error,i needs to be declared final
}
}
Inner cls=new Inner();
cls.display();
}
}
}
My questions are-
- Why the variable i needs to be declared final if I want to use it in display() method of Inner class?
- If I want to display i inside the display method in each iteration,then how may I do so?Because if I declare i to be final with initial value 0,then I will not be able to iterate over it.
Thanks .....