![](/img/trans.png)
[英]What's the best way to fix this 'write to static field from instance method' findbugs warning?
[英]FindBugs error: Write to static field from instance method
我在我的應用程序中有幾個區域,我從實例方法操作靜態變量的值時得到錯誤。
“從實例方法寫入靜態字段” 。
如果我們將多線程排除在等式之外,即使多個實例寫入相同的static
變量,這種情況是否會造成任何潛在的問題?
從文檔......
此實例方法寫入靜態字段。 如果操作多個實例並且通常是不好的做法,這很難得到糾正。
第二件事是關於提出任何潛在問題的問題
如果您正在從實例方法操作static
字段,那么類的任何對象(包含我們的實例方法的類)可能正在調用該方法,並且很難找到在某個已經存在的大型應用程序或應用程序中操作static
字段的對象由他人開發和編碼。
這個答案也可以幫到你。
編輯:
僅供參考,您可以在以下代碼中繞過findbug的警告。
class TestClass {
static int testInt = 0 ;
public static setTestInt ( int a ) {
TestClass.testInt = a ;
}
public void setInt ( int a1 ) {
setTestInt ( a1 );
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.