The Method main : Main « Language « Java Tutorial

  1. A special method called main provides the entry point to an application.
  2. An application can have many classes and only one of the classes needs to have the method main.
  3. This method allows the class containing it to be invoked.

The signature of the method main is as follows.

public class MainClass {

  public static void main(String[] args) {


  1. In addition, you can pass arguments to main when using 'java' to run a class.
  2. To pass arguments, type them after the class name. Two arguments are separated by a space.
java className arg1 arg2 arg3 ...

All arguments must be passed as strings. For instance, to pass two arguments, "1" and "mode" when running the Test class, you type this:

java Test 1 mode

1.7.1.The Method main
1.7.2.Throw Exception through main method
1.7.3.New parameter for main method
1.7.4.Display all command-line arguments.