I am not understanding the concept of final variable. In a for loop I have dynamic variable i which is crucial for me to refer an array. As soon as I use i, it throws me an error saying it should be final.
What exactly is final? Could you please help to get rid of that error?
My code is here:
for( int i = 0; i <4; i++)
{
Bitmap celeb1=Bitmap.getBitmapResource(fimagearray[i]);
Bitmap celeb1_focus=Bitmap.getBitmapResource(fimagearray[i]);
ImageButton celebbutton = new ImageButton(celeb1, celeb1_focus);
celebbutton.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context)
{
UiApplication.getUiApplication().pushScreen(new FetchTweets(fusernamearray[i]));
}
});
femaleSec.add(celebbutton);
}
An anonymous inner class can only access
finalvariables from the outer scope. Sinceiis not final, it can’t be accessed directly. You can’t makeifinal since it needs to change, and a final variable can’t be changed.As a simple workaround, copy the value of
iinto a final variable during each loop iteration: