2014-09-24 19 views
0

不习惯这是从代码的摘录:java的局部变量在Eclipse

if (resultText.equalsIgnoreCase("open browser")) 
     { 
      try{ 
      Process p; //resultText=""; 
      p = Runtime.getRuntime().exec("cmd /c start chrome.exe"); 

      }catch(Exception ae){} 
     } 

在Eclipse其显示警告如下:

The value of local variable p is not used

有什么错在这里?请帮我理解这个问题。谢谢。

回答

4

这只是IDE的警告。这不是一个错误。它只是让你知道p永远不会被使用。你可以把你的代码写入:

if (resultText.equalsIgnoreCase("open browser")) 
{ 
    try{ 
     Runtime.getRuntime().exec("cmd /c start chrome.exe"); 
    }catch(Exception ae){} 
} 

而且它会做同样的事情。

2

为什么你需要p?

Process p; //resultText=""; 
p = Runtime.getRuntime().exec("cmd /c start chrome.exe"); 

您在分配值之后未使用它。 如果使用它,警告将消失。

你的代码应该是:

if (resultText.equalsIgnoreCase("open browser")) 
    { 
     try{ 
      Runtime.getRuntime().exec("cmd /c start chrome.exe"); 
     }catch(Exception ae){} 
    } 

当你不需要变量p可言。

2

警告告诉您,您正在为p分配值,但从未使用过。

这是一个警告,所以它是可以忽略的。

0

您可以忽略警告。

它只是告诉你,你为p分配了一个值,但从未使用它。基本上

if (resultText.equalsIgnoreCase("open browser")) 
    { 
     try{ 
      Runtime.getRuntime().exec("cmd /c start chrome.exe"); 

     }catch(Exception ae){ 
      System.out.println("An error has oucurd... :d sorry"); 
     } 
    } 
+0

我不会说忽略他们,他们在那里是为了一个目的。忽略它们并不一定会伤害,但它们可能是有用的 – Michael 2014-09-24 17:51:18

+0

我的意思是*可以*忽略,因为你的程序仍然会编译并运行 – cppprog 2014-09-24 17:52:06

0

试试这个。这是你的Eclipse,警告你,你实际上并没有阅读这个变量(p),所以你没有收到任何输入。

没有在代码中使用变量p。

要删除错误 - 有必要删除变量“p”。

1

它并不是一个真正的错误: