Java 为什么在命令提示的错误消息中显示注释?
注释不应显示在错误消息中。请解释一下原因Java 为什么在命令提示的错误消息中显示注释?,java,Java,注释不应显示在错误消息中。请解释一下原因 class Test { public static void main(String args[]) { static int x= 10; //Error: Static local variables are not allowed } } o/p- 如果我们将其移动到下一行,则不会显示注释 class Test { public static void main(String args[]) { static in
class Test {
public static void main(String args[]) {
static int x= 10; //Error: Static local variables are not allowed
}
}
o/p-
如果我们将其移动到下一行,则不会显示注释
class Test {
public static void main(String args[]) {
static int x= 10;
//Error: Static local variables are not allowed
}
}
o/p->
C:\Users\hp\Desktop\Programs>javac Test.java
Test.java:3: error: illegal start of expression
static int x= 10;
^
1 error
java编译器只显示包含错误的整行代码。包含注释有助于您查看它在冗长的源文件中的哪一行。注释不应显示在错误消息中。为什么不呢?你不能用static在一个方法中声明一个变量。当我们添加注释时,它不会显示在控制台中。当Java引用一行代码作为错误时,如果有注释,它不会删除注释。什么使你相信它会或应该?在你的第二个例子中,注释不会显示,因为它不再与错误在同一行。错误消息显示发生错误的整行。没有理由显示下一行。
C:\Users\hp\Desktop\Programs>javac Test.java
Test.java:3: error: illegal start of expression
static int x= 10;
^
1 error