差異處
這裏顯示兩個版本的差異處。
— |
java:sonarlint:fixcodesmell:s2696 [2023/06/25 09:48] (目前版本) |
||
---|---|---|---|
行 1: | 行 1: | ||
+ | {{tag>SonarLint}} | ||
+ | ====== SonarLint | Instance methods should not write to "static" fields (java:S2696) ====== | ||
+ | ===== Problem ===== | ||
+ | 這個code smell是再說,使用non-static method去修改static的變數: | ||
+ | <code java> | ||
+ | private static Boolean isDelete = false; | ||
+ | |||
+ | public String apply(String content, String url, boolean isDel) { | ||
+ | isDelete = isDel; | ||
+ | // skip | ||
+ | } | ||
+ | </code> | ||
+ | 這意味著可能會因為multi-thread或者是不同instance物件共享到同一個static member,而產生資料錯亂的問題。 | ||
+ | ===== How to fix? ===== | ||
+ | 這個問題解決方法取決於你的程式邏輯。有幾個可能做法: | ||
+ | - 將static拿掉。 | ||
+ | - 將修改的method改為有同步保護的static method,例如synchronized static。 | ||
+ | ===== ===== | ||
+ | ---- | ||
+ | \\ | ||
+ | ~~DISQUS~~ |