Pregunta

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.

¿Fue útil?

Solución

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.

Otros consejos

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top