Java 在 Eclipse 中出错:预期令牌启动标识符出现语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21990742/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:53:31  来源:igfitidea点击:

Getting error in Eclipse: syntax error on token start identifier expected

javaeclipse

提问by Mistu4u

I am getting a strange error while creating a simple thread program in JAVA using Eclipse. The code is:

我在使用 Eclipse 在 JAVA 中创建一个简单的线程程序时遇到一个奇怪的错误。代码是:

package threadshow;

public class Thread_Show extends Thread{

public void run(){
    System.out.println("Inside the thread");
}
 }

 class Thread_Definition{
Thread_Show ts=new Thread_Show();
ts.start();  //Getting the error here
}

I am getting error "syntax error on token start identifier expected" in the line ts.start();. Why am I getting this?

我在行中收到错误“预期令牌起始标识符的语法错误” ts.start();。为什么我得到这个?

EDITI have used the code from http://tutorials.jenkov.com/java-concurrency/creating-and-starting-threads.html#thread-subclass

编辑我使用了来自http://tutorials.jenkov.com/java-concurrency/creating-and-starting-threads.html#thread-subclass的代码

采纳答案by Mistu4u

Found a very bad mistake done my me. Forgot to add public static void main(String args[])in the Thread_Definition class.

发现我犯了一个非常严重的错误。忘记添加public static void main(String args[])Thread_Definition 类。

回答by Jakub H

You can't start your method inside class. Create some method first.

你不能在课堂上启动你的方法。首先创建一些方法。

回答by user3213851

Are you defining both of your classes in the same java file?. If so, you define both the classes in different java files naming Thread_show and Thread_definition. Then inside Thread_definition you can create an object of Thread_show and call its function.

您是否在同一个 java 文件中定义了两个类?如果是这样,您在不同的 java 文件中定义这两个类,命名为 Thread_show 和 Thread_definition。然后在 Thread_definition 中,您可以创建 Thread_show 的对象并调用其函数。