Understand the main method of a Java class

The  main method is the start point of any Java executable application. It has the signature :

or,

The following rules apply when creating the main method :

  1. The main method must be marked as public and static. The order in which public and static may appear doesn’t matter;
  2. The name of the main method must be main;
  3. The return type of the main method must be void;
  4. The method argument must be an array of Strings or a variable argument of type String (i.e., varargs).

NOTE : The elipsis (i.e., …) must follow the type of the variable, and not the variable itself.

Observations :

  • It is not mandatory for the name of the variable passed to the main method to be args. It can be any valid Java Identifier;
  • In case you are use an array of Strings as parameter, the square brackets, [], either the type of the variable or its name;
  • The order of the keywords, public and static can be changed (i.e., use static public instead of public static).

NOTE : The main method is a static method and for this reason only static members are accessible from the main method More there is no this available to main method.

Add a Comment

Your email address will not be published.