在实例方法中写入静态变量,为什么这是一个不好的做法? [英] Writing to a static variable in an instance method, why is this a bad practice?

查看:160
本文介绍了在实例方法中写入静态变量,为什么这是一个不好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个在日食中的这个findbugs警告感到有些困惑。

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

public class MyClass {
    public static String myString;
}


public class AnotherClass {
   public void doSomething() {
       MyClass.myString = "something";
   }
}

这给了我一个findbugs警告写入静态字段从实例方法,但是这不会给我一个警告:

This gives me a findbugs warning "write to static field from instance method", however this does not give me a warning:

public class MyClass {
    public static String myString;
}


public class AnotherClass {
   public void doSomething() {
       doAnotherThing();
   }
   public static doAnotherThing() {
       MyClass.myString = "something";
   }
}

这有什么不同?,为什么写作从一个实例方法的静态变量一个坏的做法?,我认为它与同步有关,但我仍然不清楚。

How is this any different?, and why is writing to a static variable from an instance method a bad practice?, I assume it has to do with synchronization, but it is still not clear to me.

我知道这看起来像变量应该是final,但是我从属性文件加载值。

I know this looks like the variable should be final, but I am loading the value from a properties file.

推荐答案

它是一种别名形式,可能违反直觉。反直觉代码阻碍了维护的简便性。

Its a form of aliasing, which may be counter-intuitive. Counter-intuitive code hampers ease of maintenance.

从逻辑上讲,我们希望实例方法能够影响该实例的数据。我们希望静态方法会影响静态数据。

Logically, we expect instance methods to affect that instance's data. We expect static methods to affect static data.

让我们将 doSomething 重命名为 initialize

...
a.initialize();
...
b.initialize();
...

此代码的读者可能无法立即意识到<的实例code> a 和 b 实际上影响了相同的数据。这可能是一个错误,因为我们正在初始化相同的内存两次,但它不明显,因为我们可能需要在每个实例上调用 initialize 似乎是合理的。

The reader of this code may not immediately realize that the instances of a and b are actually affecting the same data. This may be a bug since we're initializing the same memory twice, but its non-obvious since it seems reasonable that we may need to call initialize on each instance.

然而,代码是:

...
MyClass.initialize();
...
MyClass.initialize();
...

在这种情况下,它更直观,我们可能会影响到相同的静态数据,这可能是一个错误。

In this case, its more intuitive that we're likely affecting the same static data and this is likely a bug.

这类似于别名的常见版本,其中同一范围内的两个变量指向同一个实例。

This is similar to the common version of aliasing where two variables in the same scope point to the same instance.

对于你的上一个例子,


  • 实例调用静态方法

  • an instance calls a static method

实例方法调用静态方法的事实不会引发标志。这个例子很有用,远远超过它可能存在的问题。

The fact that an instance method is calling a static method isn't expected to raise flags. The examples were this is useful far outweigh where its likely a problem.

一个类的静态方法会影响另一个类的静态数据

a static method of one class affects another class' static data

从某种意义上说,它应该生成一个不同但相似的警告:一个类正在弄乱另一个类的数据。但是,通过使静态变量公开是一种默认的方式,所以不需要这样的警告。

In one sense, it should generate a different, but similar warning: that one class is messing with the data of another class. However, by making the static variable public is a way of tacitly approving of this, so such a warning isn't necessary.

请记住,FindBugs只是试图在代码中标记潜在的可能问题,而不是每个可能的问题。您的第一个示例可能是一个潜在的维护问题,您需要检查它是否是一个真正的问题。你的第二个例子可能不是问题,或者它是一个真正的问题,与问题的用例过于相似。

Keep in mind that FindBugs is simply trying to flag potential likely problems, not every possible problem, in your code. Your first example is likely a potential maintenance issue that you need to examine whether its a real problem. Your second example is likely not a problem or it is a real problem that is too similar to use cases where it is not a problem.

这篇关于在实例方法中写入静态变量,为什么这是一个不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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