Issue with used variable in inner loop.
Timer innerTimer = new Timer() {
@Override
public void run() {
for(final Rectangle rectangle : rectangles) {
if(stopTimer) {
this.cancel();
innerTimer = null; // It shows "Cannot refer to a non-final variable innerTimer inside an inner class defined in a different method"
break;
}
}
}
};
If I changed The above code as :
final Timer innerTimer = new Timer() {
@Override
public void run() {
for(final Rectangle rectangle : rectangles) {
if(stopTimer) {
this.cancel();
innerTimer = null; // "The final local variable innerTimer cannot be assigned, since it is defined in an enclosing type"
break;
}
}
}
};
I am using this timer from GWT (com.google.gwt.user.client.Timer).
How to intialized variable in inner loop?
You could wrap the Timer object. E.g.
public class TimerWrap {
Timer timer = new Timer();
public void cancel() {
timer.cancel();
timer = null;
}
}
and use it like this
final TimerWrap innerTimer = new TimerWrap() {
public void run() {
for (final Rectangle rectangle : rectangles) {
if (stopTimer) {
this.cancel();
break;
}
}
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With