[M D DLS] Dead store to local variable [DLS_DEAD_LOCAL_STORE]
This instruction assigns a value to a local variable, but the value is not read or used in any subsequent instruction. Often, this indicates an error, because the value computed is never used.
Note that Sun's javac compiler often generates dead stores for final local variables. Because FindBugs is a bytecode-based tool, there is no easy way to eliminate these false positives.
看下面代碼:
public static void main(String args[]) throws Exception{
Object str = new Object() ; //報錯處
str = new Object() ;
System.out.println(str);
}
Object str = new Object();是無用的代碼,因為在下面有一句str= new Object();,很多語言編譯器它都會做優化,比如:去除一些無用的代碼以提高效率。JAVA編譯器也會做一些優化,但是,Java編譯器對上面這段代碼卻沒有做優化(你可以DComplie確認一下),編譯后的.class文件還是new了兩次,具體什么原因導致它不去做這個優化我還不能確定,我覺得難做這種優化不是借口,起碼不應該是Sun的借口。
修改這段代碼方法很簡單,隨便去掉一行new Object();就可以了。