令牌上的 Java 语法错误.... 此令牌后应有标识符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28720949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java syntax error on token.... Identifier expected after this token
提问by Nitin
I am getting Java
我正在学习 Java
Syntax error on token "callMe", Identifier expected after this token
令牌“callMe”上的语法错误,此令牌后应有标识符
on below line of my program:
在我的程序的下面一行:
c1.callMe();
class Class2 {
Class1 c1 = new Class1();
c1.callMe();
}
public class Class1 {
public void callMe() {
System.out.println("I am called!!");
}
}
回答by jpo38
Class1 c1 = new Class1();
c1.callMe();
Must be moved to a method, it can't be at the class definition level, else it makes no sense (when would your code be executed??):
必须移到方法上,不能在类定义级别,否则没有意义(你的代码什么时候执行??):
public class Class2 {
public void doSomething() {
Class1 c1 = new Class1();
c1.callMe();
}
}
回答by riflehawk
Here is how you write classes correctly in Java :)
以下是您在 Java 中正确编写类的方法:)
class Class2 {
Class1 c1 = new Class1();
public void callMe(){
c1.callMe();
}
}
public class Class1 {
public void callMe() {
System.out.println("I am called!!");
}
}
回答by Ameer Sabith
Add Main method and re-arrange your code:
添加 Main 方法并重新排列您的代码:
public class Class2 {
public static void main(String[] args) {
Class1 c1 = new Class1();
c1.callMe();
}
}
class Class1 {
void callMe(){
System.out.println("I am called!!");
}
}