Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entry point for Java applications: main(), init(), or run()?

Tags:

java

jvm

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

like image 243
Ziggy Avatar asked Nov 04 '08 10:11

Ziggy


People also ask

What is the entry point of a Java application?

The main method is the entry point of a Java application.

Why is main () the port of entry for the execution of a Java application?

The main method in Java is public so that it's visible to every other class, even which are not part of its package. if it's not public JVM classes might not able to access it. 2. The main method is static in Java so that it can be called without creating any instance.

Which method is the starting point of execution in Java?

The main() method is the entry point for a Java program. It is the first method that is invoked when a Java program is run.

Does Java need a main method to run?

The main method is not needed in java programs. As others have pointed out, web applications do not use the main method.


2 Answers

This is a peculiar question because it's not supposed to be a matter of choice.

When you launch the JVM, you specify a class to run, and it is the main() of this class where your program starts.

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.

By run(), I assume you mean the method of Runnable. This is the method invoked when a new thread is started.

  • main: program start
  • init: applet start
  • run: thread start

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.

like image 69
Jegschemesch Avatar answered Sep 28 '22 14:09

Jegschemesch


The main method is the entry point of a Java application.

Specifically、when the Java Virtual Machine is told to run an application by specifying its class (by using the java application launcher), it will look for the main method with the signature of public static void main(String[]).

From Sun's java command page:

The java tool launches a Java application. It does this by starting a Java runtime environment, loading a specified class, and invoking that class's main method.

The method must be declared public and static, it must not return any value, and it must accept a String array as a parameter. The method declaration must look like the following:

public static void main(String args[]) 

For additional resources on how an Java application is executed, please refer to the following sources:

  1. Chapter 12: Execution from the Java Language Specification, Third Edition.
  2. Chapter 5: Linking, Loading, Initializing from the Java Virtual Machine Specifications, Second Edition.
  3. A Closer Look at the "Hello World" Application from the Java Tutorials.

The run method is the entry point for a new Thread or an class implementing the Runnable interface. It is not called by the Java Virutal Machine when it is started up by the java command.

As a Thread or Runnable itself cannot be run directly by the Java Virtual Machine, so it must be invoked by the Thread.start() method. This can be accomplished by instantiating a Thread and calling its start method in the main method of the application:

public class MyRunnable implements Runnable {     public void run()     {         System.out.println("Hello World!");     }      public static void main(String[] args)     {         new Thread(new MyRunnable()).start();     } } 

For more information and an example of how to start a subclass of Thread or a class implementing Runnable, see Defining and Starting a Thread from the Java Tutorials.


The init method is the first method called in an Applet or JApplet.

When an applet is loaded by the Java plugin of a browser or by an applet viewer, it will first call the Applet.init method. Any initializations that are required to use the applet should be executed here. After the init method is complete, the start method is called.

For more information about when the init method of an applet is called, please read about the lifecycle of an applet at The Life Cycle of an Applet from the Java Tutorials.

See also: How to Make Applets from the Java Tutorial.

like image 25
coobird Avatar answered Sep 28 '22 14:09

coobird