Cannot refer to a non-final variable inside an inner class defined in a different method

前端 未结 20 2277
一向
一向 2020-11-21 05:04

Edited: I need to change the values of several variables as they run several times thorugh a timer. I need to keep updating the values with every iteration through the timer

20条回答
  •  终归单人心
    2020-11-21 05:25

    If you want to change a value in a method call within an anonymous class, that "value" is actually a Future. So, if you use Guava, you can write

    ...
    final SettableFuture myvalue = SettableFuture.create();
    ...
    someclass.run(new Runnable(){
    
        public void run(){
            ...
            myvalue.set(value);
            ...
        }
     }
    
     return myvalue.get();
    

提交回复
热议问题