There is a difference in the bytecode:
Source:
final int value; public TestBox() { value = 7; }
addPropertyChangeListener following code from addPropertyChangeListener :
0: getstatic #3; 3: aload_0 4: getfield #2; 7: invokevirtual #4;
And the source code:
final int value = 7; public TestBox() { }
addPropertyChangeListener following code from addPropertyChangeListener :
0: getstatic #3; 3: bipush 7 5: invokevirtual #4;
So there is a slight difference. But not practical.
It seems that the compiler can treat the final variable as a constant if it is intuitively defined in the definition. Of course, different compilers can do this differently.
source share