简体   繁体   English

修复此“从实例方法写入静态字段”findbugs 警告的最佳方法是什么?

[英]What's the best way to fix this 'write to static field from instance method' findbugs warning?

I have a class that looks similar to this, and findbugz is complaining about the 'write to the static field from the instance method' ( initialize() , and killStaticfield() ).我有一个与此类似的类, findbugz 抱怨“从实例方法写入静态字段”( initialize()killStaticfield() )。 I can't set the static field in the ctor.我无法在 ctor 中设置静态字段。

  • What is the best fix for this issue?解决此问题的最佳方法是什么?
  • Would putting staticField in an AtomicReference suffice?将 staticField 放在 AtomicReference 中就足够了吗?

     public class Something { private static SomeClass staticField = null; private AnotherClass aClass; public Something() { } public void initialize() { //must be ctor'd in initialize aClass = new AnotherClass(); staticField = new SomeClass( aClass ); } public void killStaticField() { staticField = null; } public static void getStaticField() { return staticField; } }

Staying as close as possible to your original design...尽可能接近您的原始设计...

public class Something {
  private static volatile SomeClass staticField = null;

  public Something() {
  }

  public static SomeClass getStaticField() {
    if(Something.staticField == null)
      Something.staticField = new SomeClass();;
    return Something.staticField;
  }
}

Refer to your static variable via the class name, that will remove the findbugz warning.通过类名引用您的静态变量,这将删除 findbugz 警告。 Mark your static variable as volatile, which will make the reference safer in a multithreaded environment.将您的静态变量标记为 volatile,这将使引用在多线程环境中更安全。

Even better would be:更好的是:

public class Something {
  private static final SomeClass staticField = new SomeClass();

  public Something() {
  }

  public static SomeClass getStaticField() {
    return Something.staticField;
  }
}

The question is what you want to do with the static field.问题是你想用静态字段做什么。 If it changes for every class you create it might not be a good idea to have it static at all.如果您创建的每个类都更改了它,那么将其设为静态可能不是一个好主意。 If it gets initialized only once you should just lazily initialize it as a singleton.如果它只被初始化一次,你应该只是懒惰地将它初始化为单例。

public class Something
{
    private static SomeClass staticField = null;

    public Something()
    {

    }

    public static SomeClass getStaticField()
    {
        if(staticField == null)
            staticField = new SomeClass();;
        return staticField;
    }
}

Remove static from staticField if it should not be static.如果它不应该是静态的,则从 staticField 中删除静态。

Make kill and getStaticField static themselves.让 kill 和 getStaticField 自己静态化。 And you usually reference static by the class name, not by an (implicit) this, to make very clear that it is static and may cause unexpected consequences in other thReads.并且您通常通过类名而不是(隐式) this 来引用 static ,以非常清楚它是静态的并且可能会在其他 thReads 中导致意外的后果。

When in doubt, don't use statics for non-constant fields.如有疑问,请勿对非常量字段使用静态。

The best way is not to do it, try to find a better design patern.最好的方法是不要这样做,尝试找到更好的设计模式。 If really necessary this will work and make findbugs/spotbugs not complain.如果真的有必要,这将起作用并使 findbugs/spotbugs 不会抱怨。

public class Something
{
    private static SomeClass staticField = null;

    public Something()
    {
    }

    private void setStaticField(SomeClass value){
        staticField=value;
    } 

    public static SomeClass getStaticField()
    {
        if(staticField == null)
            setStaticField(new SomeClass());
        return staticField;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM