Search code examples
javaandroid-studioclassoopsubclass

How could I reuse variables that are inside methods in Android Studio?


Android Studio says a variable must be declared final, so you can use it in more than one method in the same class. But is there another way to do that, because I would have to modify the variable and once you declare it final you can't change it.

This variable is inside a void onComplete(whatever) and I need to use it outside. How do we handle such things without declaring the variable final?


Solution

  • Declare the variable outside the method onComplete(). This way (as a class member) you will be able to use it in multiple methods without it being a final.

    For example:

    static double number;