posted 4 years ago
If the same instance of the class is used in multiple threads, it is not safe because the yourNumber value is can be accessed/modified by multiple threads in an uncontrolled fashion. Access to the static counter looks safe as long as the counter is only accessed via the method. That can be hard to enforce which is why I would prefer the use of a static final AtomicInteger there. You would neither be able to change the instance of the integer, nor change or get its value without going through atomically safe methods.