如何在Eclipse中隐藏侧边栏警告“未使用局部变量的值”?

问题描述 投票:6回答:2

“未使用局部变量的值”警告实际上很烦人,因为它隐藏了侧边栏中的断点。有问题的变量也会加下划线以突出显示此警告,因此侧边栏图标相当多余。

那么有没有办法在侧边栏中隐藏这个警告?

eclipse
2个回答
10
投票
  • Windows>首选项
  • Java>编译器>错误/警告
  • 打开“不必要的代码”组
  • 更改“不使用局部变量的值”从“警告”到“忽略”

它需要一个新的构建并且已经完成。

当然,您必须意识到您忽略了该选项并可能增加内存消耗并在代码中留下混乱。


5
投票

@SuppressWarnings( “未使用”)

在main()之前添加上面的代码行,它将在整个程序中禁止这种类型的所有警告。例如

public class CLineInput 
{
    @SuppressWarnings("unused")
    public static void main(String[] args) 
    {

您也可以将此添加到正在创建警告的变量的声明之上,这仅适用于特定变量的警告,而不是整个程序的警告。例如

   public class Error4 
{

    public static void main(String[] args) 
    {
        int a[] = {5,10};
        int b = 5;
        try
        {
            @SuppressWarnings("unused")     // It will hide the warning, The value of the local variable x is not used.
            int x = a[2] / b - a[1];
        }
        catch (ArithmeticException e)
        {
            System.out.println ("Division by zero");
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println("Array index error");
        }
        catch(ArrayStoreException e)
        {
            System.out.println("Wrong data type");
        }
        int y = a[1] / a[0];
        System.out.println("y = " + y);

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.