public class test {
public static void main(String[] args) throws Exception {
final int num = 111;
new Thread() {
@Override
public void run() {
num = 222;
}
}.start();
}
}
I want to change the value of num however I can only do that if I set it to final which would not let me modify this. In other languages such as C we can use pointers but Java cannot?
Java has neither closure nor pointers.
A solution would be to make the num static in the class :
Another solution would be to use an object like AtomicInteger. You can’t change the value of the variable but you can change the content of the value :