FindBugs错误:从实例方法写入静态字段 [英] FindBugs error: Write to static field from instance method

查看:514
本文介绍了FindBugs错误:从实例方法写入静态字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有几个区域在从实例方法中操纵静态变量的值时出现错误。

I have couple of areas in my application where I get the error while manipulating value of static variable from instance method.

从实例方法

如果我们不考虑多线程,即使多个实例都写入相同的 static ,这种情况也会带来任何潜在的问题

If we take multi-threading out of the equation, does this scenario pose any potential issue even if multiple instances write to the same static variable ?

推荐答案

从文档中...


此实例方法写入静态字段。如果要操纵多个实例,这很难使bbb正确,并且通常是不好的bbb实践。

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

      如果您要通过实例方法操作 static 字段,则class(包含我们的实例方法的类)可能正在调用该方法,并且在某些大型应用程序或已开发的应用程序中很难找到操作 static 字段的对象

      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.

      编辑:

      仅供参考,您可以在以下代码中绕过findbug的警告。

      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 ); 
           }
      }
      

      这篇关于FindBugs错误:从实例方法写入静态字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆