Alex Rivera | Logout

Why is it not good to write to static field from instance method in Java?

Asked 2012-07-04T08:15:30.723
10

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 ?

Edit
Report

1 Answer

0

In addition to the above, from a documentation point of view it is easy for someone to assume it is an instance field, if you use the form:

someField = null

or

myObj.someField = null

Using the form

myClass.someField = null

makes it clearer and prevents the findbugz warning (apparently, I haven't tested). Note, that won't prevent the multi-threading issues described.

answered 2012-07-04T08:33:50.187

Your Answer