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

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

Entry point for Java applications: main(), init(), or run()?

javajvm

提问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 mainmethod 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 javaapplication launcher), it will look for the mainmethod with the signature of public static void main(String[]).

具体来说,当 Java 虚拟机被告知通过指定其类(通过使用java应用程序启动器)来运行应用程序时,它将查找main具有public static void main(String[]).

From Sun's javacommand 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 Stringarray 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 应用程序的其他资源,请参阅以下来源:

  1. Chapter 12: Executionfrom the Java Language Specification, Third Edition.
  2. Chapter 5: Linking, Loading, Initializingfrom the Java Virtual Machine Specifications, Second Edition.
  3. A Closer Look at the "Hello World" Applicationfrom the Java Tutorials.
  1. 第12章:执行Java语言规范,第三版
  2. 第5章:链接,加载,初始化Java虚拟机规范,第二版
  3. 深入了解Java 教程中的“Hello World”应用程序


The runmethod is the entry point for a new Threador an class implementing the Runnableinterface. It is not called by the Java Virutal Machine when it is started up by the javacommand.

run方法是Thread实现该Runnable接口的新类或类的入口点。Java 虚拟机在通过java命令启动时不会调用它。

As a Threador Runnableitself 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 Threadand calling its startmethod in the mainmethod of the application:

由于 a ThreadorRunnable本身不能直接由 Java 虚拟机运行,因此必须由Thread.start()方法调用。这可以通过实例化 aThread并在应用程序startmain方法中调用其方法来完成:

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 Threador a class implementing Runnable, see Defining and Starting a Threadfrom the Java Tutorials.

有关如何启动 的子类Thread或实现类的更多信息和示例Runnable,请参阅Java 教程中的定义和启动线程



The initmethod is the first method called in an Appletor JApplet.

init方法是AppletJApplet 中调用的第一个方法。

When an applet is loaded by the Java plugin of a browser or by an applet viewer, it will first call the Applet.initmethod. Any initializations that are required to use the applet should be executed here. After the initmethod is complete, the startmethod is called.

当小程序被浏览器的 Java 插件或小程序查看器加载时,它会首先调用该Applet.init方法。使用小程序所需的任何初始化都应在此处执行。在之后init的方法完成,start方法被调用。

For more information about when the initmethod 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.

另请参阅:如何从 Java 教程制作小程序

回答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。