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()?


- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01