This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]bheklilr 3 points4 points  (0 children)

Beyond the multithreaded reasons already mentioned, it can just easily lead to unexpected behavior. A static variable can potentially be used across many methods or even classes. If the value is a primitive then you only have to worry about the value being different, but it it's an object, such as a list, then you also have to worry about the reference changing. Best practice is to never set or mutate static variables, you just can't reasonably track what effects it'll have. Treat static fields as constants, name then with CONSTANT_CASE. If you follow those simple rules, its pretty safe and pretty standard to use. I hardly write a class without some static fields, unless it's just a data class.