I have found a post here in SO discussing the code change required to prevent writing to a static field from an instance method but why is not a good practice to do so ? Why did Java designers allow this then ?In other words why does the compiler not throw an error when some one attempts to do this ?
In the words of the 'findbugz' documentation:
This instance method writes to a static field. This is tricky to get correct if multiple instances are being manipulated, and generally bad practice.
Which is to say it isn't always wrong, simply that it is something that is often a source of errors. Instance objects manipulating static fields can be useful for example for lazy initialization of shared objects, so it is not always wrong, but it can be hard to get it right (particularly if multiple threads may be running at the same time).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With