The final variable may not have been initialized.

I have code like this inside a method:

final TouchableSpan touchableSpan = new TouchableSpan() { @Override public void onClick(View widget) { this.setPressed(true); String extravar = touchableSpan.getMyVar(); } 

On this line, String extravar = touchableSpan.getMyVar(); A warning appears that the touchableSpan variable touchableSpan not been initialized. Why there?

This warning came when I added the final modifier. I used to have variable is access from inner class, needs to be declared final .

+6
source share
1 answer

First you create an anonymous class and then assign it to the final variable. Thus, your onClick method onClick theoretically be called before the final variable is initialized. Why not just use this.getMyVar() ?

+9
source

Source: https://habr.com/ru/post/986438/


All Articles