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

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

问题描述

我对 Eclipse 中的这个 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.

我知道这看起来变量应该是最终的,但我正在从属性文件加载值.

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:

Let's rename doSomething to initialize:

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

这段代码的读者可能不会立即意识到 ab 的实例实际上影响了相同的数据.这可能是一个错误,因为我们对相同的内存进行了两次初始化,但它并不明显,因为我们可能需要在每个实例上调用 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.

对于你的最后一个例子,

For your last example,

  • 实例调用静态方法

  • 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天全站免登陆