Java 应用程序的入口点:main()、init() 或 run()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/261428/
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
Entry point for Java applications: main(), init(), or run()?
提问by Ziggy
So far I've been using public void run() {}
methods to execute my code in Java. When/why might one want to use main()
or init()
instead of run()
?
到目前为止,我一直在使用public void run() {}
方法在 Java 中执行我的代码。何时/为什么可能要使用main()
或init()
代替run()
?
采纳答案by Jegschemesch
This is a peculiar question because it's not supposed to be a matter of choice.
这是一个特殊的问题,因为它不应该是一个选择问题。
When you launch the JVM, you specify a class to run, and it is the main()
of this class where your program starts.
当您启动 JVM 时,您指定了一个要运行的类,并且main()
您的程序在该类中启动。
By init()
, I assume you mean the JApplet method. When an applet is launched in the browser, the init()
method of the specified applet is executed as the first order of business.
通过init()
,我假设您指的是 JApplet 方法。当小程序在浏览器中启动时,init()
指定小程序的方法作为业务的第一顺序执行。
By run()
, I assume you mean the method of Runnable. This is the method invoked when a new thread is started.
通过run()
,我假设您的意思是 Runnable 的方法。这是启动新线程时调用的方法。
- main: program start
- init: applet start
- run: thread start
- main:程序启动
- init:小程序启动
- 运行:线程启动
If Eclipse is running your run()
method even though you have no main()
, then it is doing something peculiar and non-standard, but not infeasible. Perhaps you should post a sample class that you've been running this way.
如果 Eclipse 正在运行您的run()
方法,即使您没有main()
,那么它正在做一些特殊和非标准的事情,但并非不可行。也许您应该发布一个您一直以这种方式运行的示例类。
回答by Jon Skeet
The main()
method is the entry point for a Java application. run()
is typically used for new threads or tasks.
该main()
方法是 Java 应用程序的入口点。run()
通常用于新线程或任务。
Where have you been writing a run()
method, what kind of application are you writing (e.g. Swing, AWT, console etc) and what's your development environment?
您在哪里编写run()
方法,您正在编写什么样的应用程序(例如 Swing、AWT、控制台等)以及您的开发环境是什么?
回答by Matthew Schinckel
Java has a special static method:
Java 有一个特殊的静态方法:
public static void main(String[] args) { ... }
which is executed in a class when the class is started with a java command line:
当使用 java 命令行启动类时,它在类中执行:
$ java Class
would execute said method in the class "Class" if it existed.
如果存在,将在类“Class”中执行所述方法。
public void run() { ... }
is required by the Runnable interface, or inherited from the Thread class when creating new threads.
Runnable 接口需要,或者在创建新线程时从 Thread 类继承。
回答by coobird
The main
method is the entry point of a Java application.
该main
方法是 Java 应用程序的入口点。
Specifically、when the Java Virtual Machine is told to run an application by specifying its class (by using the java
application launcher), it will look for the main
method with the signature of public static void main(String[])
.
具体来说,当 Java 虚拟机被告知通过指定其类(通过使用java
应用程序启动器)来运行应用程序时,它将查找main
具有public static void main(String[])
.
From Sun's java
command page:
从 Sun 的java
命令页面:
The javatool launches a Java application. It does this by starting a Java runtime environment, loading a specified class, and invoking that class's mainmethod.
The method must be declared public and static, it must not return any value, and it must accept a
String
array as a parameter. The method declaration must look like the following:public static void main(String args[])
在Java的工具启动Java应用程序。它通过启动 Java 运行时环境、加载指定的类并调用该类的main方法来完成此操作。
该方法必须声明为 public 和 static,它不能返回任何值,并且它必须接受一个
String
数组作为参数。方法声明必须如下所示:public static void main(String args[])
For additional resources on how an Java application is executed, please refer to the following sources:
有关如何执行 Java 应用程序的其他资源,请参阅以下来源:
- Chapter 12: Executionfrom the Java Language Specification, Third Edition.
- Chapter 5: Linking, Loading, Initializingfrom the Java Virtual Machine Specifications, Second Edition.
- A Closer Look at the "Hello World" Applicationfrom the Java Tutorials.
The run
method is the entry point for a new Thread
or an class implementing the Runnable
interface. It is not called by the Java Virutal Machine when it is started up by the java
command.
该run
方法是Thread
实现该Runnable
接口的新类或类的入口点。Java 虚拟机在通过java
命令启动时不会调用它。
As a Thread
or Runnable
itself cannot be run directly by the Java Virtual Machine, so it must be invoked by the Thread.start()
method. This can be accomplished by instantiating a Thread
and calling its start
method in the main
method of the application:
由于 a Thread
orRunnable
本身不能直接由 Java 虚拟机运行,因此必须由Thread.start()
方法调用。这可以通过实例化 aThread
并在应用程序start
的main
方法中调用其方法来完成:
public class MyRunnable implements Runnable
{
public void run()
{
System.out.println("Hello World!");
}
public static void main(String[] args)
{
new Thread(new MyRunnable()).start();
}
}
For more information and an example of how to start a subclass of Thread
or a class implementing Runnable
, see Defining and Starting a Threadfrom the Java Tutorials.
有关如何启动 的子类Thread
或实现类的更多信息和示例Runnable
,请参阅Java 教程中的定义和启动线程。
The init
method is the first method called in an Appletor JApplet.
该init
方法是Applet或JApplet 中调用的第一个方法。
When an applet is loaded by the Java plugin of a browser or by an applet viewer, it will first call the Applet.init
method. Any initializations that are required to use the applet should be executed here. After the init
method is complete, the start
method is called.
当小程序被浏览器的 Java 插件或小程序查看器加载时,它会首先调用该Applet.init
方法。使用小程序所需的任何初始化都应在此处执行。在之后init
的方法完成,start
方法被调用。
For more information about when the init
method of an applet is called, please read about the lifecycle of an applet at The Life Cycle of an Appletfrom the Java Tutorials.
有关何时init
调用小程序的方法的更多信息,请在 Java 教程中的小程序生命周期中阅读有关小程序生命周期的信息。
See also: How to Make Appletsfrom the Java Tutorial.
回答by oreadings
as a beginner, i import acm packages, and in this package, run() starts executing of a thread, init() initialize the Java Applet.
作为初学者,我导入了 acm 包,在这个包中,run() 开始执行一个线程,init() 初始化 Java Applet。