java 写入静态字段 - 在这种情况下 FindBugs 是错误的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13388829/
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
Write to static field - is FindBugs wrong in this case?
提问by htorque
I have a Java class like this:
我有一个这样的 Java 类:
public class Foo {
public static int counter = 0;
public void bar(int counter) {
Foo.counter = counter;
}
}
FindBugs warns me about writing to the static field counter
via the instance method bar
. However, if I change the code to:
FindBugs 警告我不要counter
通过实例方法写入静态字段bar
。但是,如果我将代码更改为:
public class Foo {
public static int counter = 0;
public static void setCounter(int counter) {
Foo.counter = counter;
}
public void bar(int counter) {
setCounter(counter);
}
}
Then FindBugs won't complain. Isn't that wrong? I'm still writing to a static field from an instance method, just via a static method, am I not?
那么 FindBugs 就不会抱怨了。是不是错了?我仍在从实例方法写入静态字段,只是通过静态方法,不是吗?
回答by dbyrne
Suppose that at some point in the future, you decide this setter method needs to be thread safe and you want to make it synchronized
.
假设在未来的某个时刻,您决定此 setter 方法需要是线程安全的,并且您希望使其成为synchronized
。
This code will work fine:
此代码将正常工作:
public synchronized static void setCounter(int counter) {
Foo.counter = counter;
}
public void bar(int counter) {
setCounter(counter);
}
This code is wrong and will have incorrect behavior:
这段代码是错误的,会有不正确的行为:
public synchronized void bar(int counter) {
Foo.counter = counter;
}
This might not seem like a significant difference in this contrived example, especially since counter
can usually just be marked volatile
. However, in a real world example where the setter method has more complicated logic and is being called from many different places (not just from one instance method), the latter pattern will be easier to refactor.
在这个人为的示例中,这似乎没有显着差异,特别是因为counter
通常可以只标记volatile
。然而,在真实世界的示例中,setter 方法具有更复杂的逻辑并且从许多不同的地方(不仅仅是从一个实例方法)被调用,后一种模式将更容易重构。
As an aside, in my opinion Google's CodePro Analytixplugin is a much faster and more comprehensive tool than FindBugs.
顺便说一句,在我看来,谷歌的 CodePro Analytix插件是一个比 FindBugs 更快、更全面的工具。
Related:
有关的:
回答by barrowc
From the FindBugs list of bug descriptions:
从错误描述的 FindBugs 列表中:
ST: Write to static field from instance method (ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD)
This instance method writes to a static field. This is tricky to get correct if multiple instances are being manipulated, and generally bad practice.
ST:从实例方法写入静态字段 (ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD)
此实例方法写入静态字段。如果操作多个实例,这很难纠正,而且通常是不好的做法。
There is no similar bug description for access to a static field via a static method called from an instance method.
通过从实例方法调用的静态方法访问静态字段没有类似的错误描述。
You may want to discuss the rationale behind this decision on the FindBugs mailing list
您可能想在 FindBugs邮件列表上讨论此决定背后的基本原理