I am making a minesweeper game. I want to reduce the number of flags in the inner class but I get this error ."local variables referenced from an inner class must be final or effectively final java".How can I solve this problem ?
Asked
Active
Viewed 192 times
1 Answers
0
You can't reference a variable from the outer class in the inner class if it's not at least effectively final. An effectively final variable is a variable that is guaranteed to never change.
However, you can wrap your variable. Any kind or wrapper is good. You can transform 'flags' (or whatever you called your variable) into one element array and use it instead:
final int[] flags = {0};
Now you can update your variable from the inner class:
flags[0]++;
You can also use AtomicInteger
or any other kind of wrapper.

LastAirBender
- 113
- 7
-
This is really bad advice and will fail once the code in the inner class is executed *after* the containing method is already done (think callbacks etc.). – Johannes Kuhn Apr 26 '20 at 11:23
-
No, I'm saying 'flags' should be his class variable. It shouldn't be declared inside the containing method. – LastAirBender Apr 26 '20 at 11:34
-
Why are they closing my question? I asked an understandable question – Burak Apr 26 '20 at 13:18
-
Actually I have no idea... – LastAirBender Apr 26 '20 at 13:20