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()?
基础教程推荐
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- JPA惰性列表上的流 2022-01-01
