Java 从实例方法写入静态字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24703755/
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 from instance method
提问by TechCrunch
I have my code as below. I see
我的代码如下。我懂了
public MyClass{
private static DataSource dataSource = null;
private static DataSource getDataSource(){
if (dataSource == null) {
try {
dataSource = // something.
} catch (Exception e) {
// some exception.
}
}
return dataSource;
}
public List doSomething(){
// ...
if(dataSource == null){
dataSource = getDataSource();
}
dataSource.getConnection();
// ...
}
}
I see following message in sonar anaylsis.
我在声纳分析中看到以下消息。
Dodgy - Write to static field 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.
findbugs:ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD Sep12 Reliability > Architecture
I see everything is okay in this implementation except that we are changing the static variable in doSomething method. How do we fix this ?
我看到在这个实现中一切都很好,只是我们正在改变 doSomething 方法中的静态变量。我们如何解决这个问题?
采纳答案by David T.
Not sure how your static analysis tool works but -
不确定您的静态分析工具如何工作,但是 -
try writing to your value via a static setter:
尝试通过静态设置器写入您的值:
private synchronized static void setDataSource(DataSource ds) {
dataSource = ds;
}
so that you can do
这样你就可以做
if(dataSource == null){
setDataSource(getDataSource());
}
回答by Mifmif
You have to choose between two solutions , first one is in your Datasource class add getDatasource()
method and synchronize the block where you instantiate your static field :
您必须在两种解决方案之间进行选择,第一种是在您的 Datasource 类中添加getDatasource()
方法并同步您实例化静态字段的块:
private static DataSource getDataSource(){
synchronize(DataSource.class){
if (dataSource == null) {
try {
dataSource = // something.
} catch (Exception e) {
// some exception.
}
}
}
return dataSource;
}
then you will just call this method from your doSomething()
method
那么你只需从你的doSomething()
方法中调用这个方法
public void doSomething(){
//
DataSource dataSource=DataSource.getDataSource();
//
}
second solution is to instantiate your field at the class loading time using a static block or directly from the declaration.
第二种解决方案是在类加载时使用静态块或直接从声明中实例化您的字段。
static {
dataSource = // something.
}