如何避免java警告说
不使用局部变量的值
对于声明为 private
的变量?
您有多种选择:
- 删除该字段。
它是未使用的,所以它不应该在那里。 - 注释掉字段,例如使用
// TODO
适合暂时隐藏警告,直到您使用字段编写代码。 - 使用
@SuppressWarnings("unused")
禁止显示警告。 - 禁用 IDE 设置中的警告。对于 Eclipse,那将在
-
Window
>Preferences
-
Java
>Compiler
>Error/Warnings
-
Unnecessary code
>Unused private member
- 选择选项
Ignore
-
但是,对于#3和#4,尽管您可以,但为什么要这样做?
由于它未被使用并且不包含您感兴趣的任何代码,因此您可以将其删除。
这是因为变量或变量的值未在程序中使用。因此,要删除此警告,您只需删除此变量即可,因为您没有在任何地方使用它。或者在代码中的某个时刻使用此变量
实际上,在某些情况下,这并不容易。下面是一个示例,说明为什么它不像注释掉代码那么容易。当然这是一个评论,但我没有 50 个代表可以评论,评论块无论如何都是蹩脚的。所以起诉我。顺便说一句,这不起作用,但这里的评论很蹩脚。
try {
// so, rather than a wait, we check for exit value
// and if that tosses an exception, the process is
// still running. Ooooooookkkkkaaaaaayyyyyy No Problem
int exitValue = pShowProcess.exitValue();
// guess we don't do this to get rid of the not referened warning
//(void)exitValue;
// we don't care what the exit value was
exitValue = 0;
// but if we get here, then the show stopped, so
// if we stop it now, it won't need to wait, it will be fine
// we think.
endShow();
} catch (Exception ex) {
// Process is still running. So just keep going until
// mouse clicks or something else stops the show
}