问题描述
到目前为止,我一直在使用 public void run(){}
方法在Java中执行我的代码。何时/为什么可能想要使用 main()
或 init()
而不是运行()
?
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方法。当在浏览器中启动applet时,指定applet的 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 :applet start
- run:thread start
如果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()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!