Writing to a static variable in an instance method, why is this a bad practice?

后端 未结 6 1880
故里飘歌
故里飘歌 2020-12-15 06:43

I am a little confused here with this findbugs warning in eclipse.

public class MyClass {
    public static String myString;
}


public class AnotherClass {         


        
6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 07:04

    This is my take, so take it with a grain of salt. You mentioned synchronization issues, which are a major reason for this warning, but more importantly, the two cases are fundamentally operating on different conceptual "levels" of data. Instance methods are "owned" by objects and modify data that describes individual instances. Class methods are generic operations and state that, while related to the class, are not related to individual objects. Thus, modifying that state from within each instance would probably (but not necessarily) be a poor design decision.

提交回复
热议问题