英文:
Not A Statement Error When Instantiating Class in Main Method
问题
我在MyClass class = new MyClass();
处得到了一个“不是语句”的错误,但我无法弄清楚原因。有人可以指出问题吗?
英文:
import java.util.*;
public class MyClass {
private Map<String,Integer> map;
public MyClass() {
map = new HashMap<>();
map.put("foo", 1);
map.put("bar", 3);
}
public static void main(String[] args) {
System.out.println("hi");
MyClass class = new MyClass();
}
}
I am getting a "not a statement" error at MyClass class = new MyClass();
but I can't figure out why. Can someone point out the issue please?
答案1
得分: 3
不要将 "class" 用作对象名称。请将其重命名为其他名称。
"class" 是一个关键字,因此编译器期望在它之后是类的名称(更确切地说,它期望是一个完整有效的类声明)。因此,它无法将您的行定义为有效的语句。
英文:
Don't use "class" as an object name. Rename it to something else.
class
is a keyword, so compiler is expecting the name of a class after it (To be more exact, it is expecting complete valid declaration of a class). Therefore, it cannot define your line as a valid statement.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论