public static void main(String args[]) throws Exception {
int maxScore = 0;
Thread student = new Thread(client,????);
student.start();
}
I want student thread to change value of maxScore, how do I do it in Java? (Like in C we can pass the address of maxScore)
You can’t. There is no way you can change the value of a local variable from another thread.
You can, however, use a mutable type that has an
intfield, and pass it to the new thread. For example:(Apache commons-lang provide a
MutableIntclass which you can reuse)Update: for a global variable you can simple use
public staticfields. Note that if you are willing not only to store some values in them, but also read them and do stuff depending on that, you would need to usesynchronizedblocks, orAtomicInteger, depending on the usages.