If the field is private, no other object can get at it, so it's safe. That's not quite right. The private field can be (and most likely is) modified from a public method (or from a private method that is called from a public method).
Making it static makes it thread-unsafe, because other objects of the same class can get at it via aThread.aNumber That is also not quite right. The fact is, the getNumber() method above is unsafe no matter if the
aNumber field is static or not. Here is some code to demonstrate the idea:
The output is likely to be different every time you run this
test. Here is what I got from two separate runs:
run1: Expected: 100000000 Actual: 45381942
run2: Expected: 100000000 Actual: 72689195
[ June 30, 2005: Message edited by: John Smith ]