Java 应用程序的入口点:main()、init() 还是 run()? [英] Entry point for Java applications: main(), init(), or run()?

查看:24
本文介绍了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()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆