FindBugs error: Write to static field from instance method

女生的网名这么多〃 提交于 2019-12-03 07:04:58
Not a bug

From the 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.

  • Firstly it says that it is a bad practice, not incorrect.
  • Second thing is the question, about posing any potential issue

    If you are manipulating a static field from instance method, any object of class( class that contains our instance method) may be calling that method and it will be difficult to find out the object manipulating static field in some large application or application that is already developed and coded by others.

This Answer may help you also.

EDIT :

FYI, You can bypass the warning of findbug in following code.

class TestClass {

     static int testInt = 0 ;

     public static setTestInt ( int a ) {
          TestClass.testInt = a ;
     }

     public void setInt ( int a1 ) {
          setTestInt ( a1 ); 
     }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!