As far as I know to inside methods in anonymous inner class can use final varibles or class fields. Is there a significant difference between them? for example:
final int[] intArr = new int[1];
Button testButton1 = (Button) findViewById(R.id.btnTest1);
testButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intArr[0]++;
Log.i("test", String.valueOf(intArr[0]));
}
});
Button testButton2 = (Button) findViewById(R.id.btnTest2);
testButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intArr[0]--;
Log.i("test", String.valueOf(intArr[0]));
}
});
I have 2 buttons and both them use intArr[0] and can get and set it value. The same behavior will be if I replace intArr[0] with some class fields like private int value; Based on this, I conclude that class fields and final variables are basically the same (I mean they are represented equally in the bytecode) and have difference only in scope and and the possibility to assign a value. Am I right?