Entry point for Java applications: main(), init(), or run()?(Java 应用程序的入口点:main()、init() 还是 run()?)
问题描述
到目前为止,我一直在使用 public void run() {}
方法在 Java 中执行我的代码.何时/为什么要使用 main()
或 init()
而不是 run()
?
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()
?
推荐答案
这是一个特殊的问题,因为它不应该是一个选择问题.
This is a peculiar question because it's not supposed to be a matter of choice.
当你启动JVM时,你指定一个类来运行,它是这个类的main()
你的程序开始的地方.
When you launch the JVM, you specify a class to run, and it is the main()
of this class where your program starts.
通过 init()
,我假设您的意思是 JApplet 方法.当在浏览器中启动小程序时,指定小程序的init()
方法作为第一业务顺序执行.
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.
run()
,我假设你的意思是 Runnable 的方法.这是新线程启动时调用的方法.
By run()
, I assume you mean the method of Runnable. This is the method invoked when a new thread is started.
- main:程序开始
- init:小程序启动
- 运行:线程启动
如果 Eclipse 正在运行您的 run()
方法,即使您没有 main()
,那么它正在做一些特殊且非标准的事情,但并非不可行.或许您应该发布一个您一直以这种方式运行的示例类.
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.
这篇关于Java 应用程序的入口点:main()、init() 还是 run()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 应用程序的入口点:main()、init() 还是 run()?


基础教程推荐
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01