I have a local variable in my outer method that I want to change from an anonymous inner class. How can I do it?
I tried the solution using a one element array described here
public class outerClass{
static public void outerMethod(Interface interface) {
final String[] variable = new String[1];
new Thread(new Runnable() {
@Override
public void run() {
variable[0] = "Hello";
Log.i("test", variable[0]); // Works, prints "Hello"
}
}).start();
Log.i("test", variable[0]); // Doesn't work, null string
}
}
and the solution using a holder described here
public class outerClass{
static public void outerMethod(Interface interface) {
final Holder<String> variable = new Holder<String>;
new Thread(new Runnable() {
@Override
public void run() {
variable.held = "Hello";
Log.i("test", variable.held); // Works, prints "Hello"
}
}).start();
Log.i("test", variable.held); // Doesn't work, null string
}
}
class Holder<String> {
public String held;
}
but both don't work in my case for some reason.
It might be relevant, but what is different is that my outer method is static. I also simplified my code here, the original code was for an anonymous Callback class from the Retrofit library on Android.