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


Answers

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.

Question

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




It invokes this method by using reflection. You could take a look into Java Web Start sources for example




How does the JVM use the “Main method” to start a Java program?

main doesn't need to be a keyword in java in order for the JVM to look for it at the start of execution. There is no conflict with other methods or variables also called main. This is simply how the JVM spec was designed. It was most likely borrowed from the c language.

Java Specification References: keywords, invoking main.




The JVM is looking for a very special main method to run. Only the signature

public static void main( String[] args )

is found. All other methods with name main are just "normal" methods.




It's usually main. But in this program, it's pain:

public class WhatThe {
    public static final int x = pain();
    public static int pain() {
        System.out.println("pain!");
        return 0;
    }
    public static void main(String[] args) {
        System.out.println("main");
    }
}

As it is in this one:

public class WhatThe {
    static {
        pain();
    }
    public static void pain() {
        System.out.println("pain!");
    }
    public static void main(String[] args) {
        System.out.println("main");
    }
}

This is unlikely to be useful knowledge, but it's something to be aware of.




Which is the first method called when Java executes a program?

In addition to aioobes answer

A usual way to start a simple java program is to execute java like this:

java com.example.MyClass

com.example.MyClass (or your fully qualified class name) needs to have a main method with exactly this signature:

public static void main(String[] args)

(you're only allowed to change the name of the parameter, like arguments instead of args). The virtual machine will try to load the named class and try to invoke this static method which will "start the Java program".







Tags