1. General programming advice
This message is there to warn you about a potential programming mistake because it is a common pitfall for beginner programmers, who are not aware of the differences in the scope of static and instance variables.
However, if you can declare your real version of the removeApple
method static
without causing any compiler errors, then most probably you should. This would both take care of the warning and make it clear that this method has nothing to do with any specific instance of your class.
2. Concerns related to concurrency
Another aspect of this warning concerns thread safety. If you write to a static
field from an instance, that opens the possibility that you'll be doing concurrent updates from different threads, even with no sharing of the class instances between threads.
If you don't need thread safety for your code (which is perfectly fine in general), then you don't need to do anything. If you do need it, then synchronize all updates of the field, or use an AtomicInteger
wrapper.
Personally, I'd opt for AtomicInteger
because it's the safest option: other options would need you to chase all field updates around the class and make sure they are synchronized. Using AtomicInteger
is very simple:
private static final AtomicInteger count = new AtomicInteger();
and then you use count.getAndIncrement()
instead of count++
.