Not A Statement Error When Instantiating Class in Main Method

huangapple go评论79阅读模式
英文:

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.

List of words that you cannot use as variables' names.

huangapple
  • 本文由 发表于 2020年8月14日 04:39:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63402885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定