我有这种方法;
private static String formatVolumeForLife(String rateBasisValue, String volumeValue) {
NumberFormat formatter = new DecimalFormat();
if(PER_UNIT.equals(rateBasisValue) || PER_DEPENDENT_PER_UNIT_PER_MONTH.equals(rateBasisValue)){
formatter = new DecimalFormat(FORMAT_NUMBER_WITH_COMMA);
} else {
formatter = new DecimalFormat(FORMAT_CURRENCY);
}
String formattedVolume = formatter.format(Double.parseDouble(volumeValue));
return formattedVolume;
}
Sonar抱怨“Sonar dead store to local variable formatter”。
我是否需要更改NumberFormat formatter = new DecimalFormat();?
答案 0 :(得分:3)
永远不会使用创建的对象。将代码更改为
private static String formatVolumeForLife(String rateBasisValue, String volumeValue) {
NumberFormat formatter;
if(PER_UNIT.equals(rateBasisValue) || PER_DEPENDENT_PER_UNIT_PER_MONTH.equals(rateBasisValue)){
formatter = new DecimalFormat(FORMAT_NUMBER_WITH_COMMA);
} else {
formatter = new DecimalFormat(FORMAT_CURRENCY);
}
return formatter.format(Double.parseDouble(volumeValue));
}