문제

Hi am new to android development and have been watching The New Bostons Videos and have a question about the final modifier. Why when I type something like this

TextView display = (TextView) findViewById(R.id.tvResults);
display.setText("LEFT!!!"); 

in to my OnCreate class do I need to add the final modifier to display.

도움이 되었습니까?

해결책

You don't have to use the final modifier but it is good coding practice, for any Java variable, to use the final modifier if you don't intend to ever assign another object to the variable. This means that, if you see it assigned at the top of the method, as another developer you can skim through loads of code without worrying that the variable might have been reassigned somewhere else in the method.

다른 팁

No you don't final variable's value cannot be changed during runtime its only can be initialized so if you

final TextView display = (TextView) findViewById(R.id.tvResults);
display.setText("LEFT!!!"); 

then write

display = (TextView) findViewById(R.id.textview1);

this will give you compilation error

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top