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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3630485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:57:18  来源:igfitidea点击:

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

javafindbugs

提问by darrickc

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()). I can't set the static field in the ctor.

我有一个与此类似的类,而 findbugz 抱怨“从实例方法写入静态字段”(initialize(), 和killStaticfield())。我无法在 ctor 中设置静态字段。

  • What is the best fix for this issue?
  • Would putting staticField in an AtomicReference suffice?

     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;
      }
    }
    
  • 解决此问题的最佳方法是什么?
  • 将 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;
      }
    }
    

采纳答案by romacafe

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. Mark your static variable as volatile, which will make the reference safer in a multithreaded environment.

通过类名引用您的静态变量,这将删除 findbugz 警告。将您的静态变量标记为 volatile,这将使引用在多线程环境中更安全。

Even better would be:

更好的是:

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

  public Something() {
  }

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

回答by Daff

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

回答by extraneon

Remove static from staticField if it should not be static.

如果它不应该是静态的,则从 staticField 中删除静态。

Make kill and getStaticField static themselves. 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.

让 kill 和 getStaticField 自己静态化。并且您通常通过类名而不是(隐式) this 来引用 static ,以非常清楚它是静态的并且可能会在其他 thReads 中导致意外的后果。

When in doubt, don't use statics for non-constant fields.

如有疑问,请勿对非常量字段使用静态。