Monday , November 20 2017
Home / java / Java Main Method – public static void main(String args[])

Java Main Method – public static void main(String args[])

In Java main method is an entry point of any core Java program. Why I am mention specifically as core Java program is, in other than core Java programs like Servlets, applets and any Java based frameworks, can have their own life cycles and have their own entry points.

For example in servlet programming we don’t have any main method, but it is not mean that there is no entry point for a servlet, the starting point of a servlet is init().

public static void main(string[] args)

In Java main method is called by the main Thread, which is created by the JVM. And the program containing the main method runs until the main thread is alive. After completion of the main thread the program will be terminated.

java classname

When we hit the above (class name followed by the “java”) command, the JVM checks for the main method with the default prototype.

Default prototype for main method:

public static void main(String args[])

Why Java main method is public ?

main method should be public, “public” is the access modifier in java. By making the main method as public, the method can get access from any ware, any package. For instance, consider in your system JVM is installed under the “C:\” directory but the java program containing the main method is in the “D:\” directory, by making the main as public the JVM will allowed to get access the main method from “C:\” to “D:\”.

Why main method is static ?

Static is one of the access modifier in Java. When we declare a  variable, method, block, class with static modifier those are all under class level access, and not for a specific instance or object. Hence the static functionalities are directly accessed by the class name. For direct accessing with JVM, the main method should be static. Hence JVM will call the main method directly by using the class only, for that JVM will not create any object for the class.

Why main method doesn’t return any value ?

When ever we declare a method with “int” or any return type except “void”, that means we can catch hold of that particular return value and make use of that value from called method and do something on that value. But the main method is called by the JVM, and JVM will not expect the value from the main method as a return. Because there is no more functionality done by the JVM after getting the return value from the main, hence the main method is declared as “void”.

Why the main method name declared as “main” ?

In JVM the main method name is configured as a “main”. If we change the configuration of JVM with respect to the main method, then we can change the main method name as we want.

What is String[] args ?

In Java main method takes the array of Strings as a parameter. By using these parameter we can pass the command line arguments to the main.

The possible customizations on main method

  1. public static void main(String args[]);
  2. public static void main(String[] args);
  3. public static void main(String []args);
  4. public static void main(String… args);
  5. static public void main(String args[]);
  6. public static void main(int[] args);

Do you know?

  1. we can make the main method as final.
  2. we can make the main method synchronize.
  3. we can make the main method as strictfp (strict floating point).
  4. we can overload the main method.
  5. we can also override the main method but it termed as data hiding (not overriding).

Hence the below syntax is applicable for main method:

final static synchronize strictfp public void main(String args[]){}

Overloading main method

we can overload the main method, and also we can call the main method if need.

Example:


public class Parent {

    public static void main(String[] args) {
        System.out.println("Sring args::");
    }

    /**
     * Overloading main method
     */
    public static void main(int[] args) {
        System.out.println("int args::" + args[0] + "" + args[1] + "" + args[2]);
    }
}

class Child {

    public static void main(String[] args) {
        int[] intArgs = {1, 2, 3};
        Parent.main(intArgs);
    }
}

By running the Child class we can get the bellow output.

Output:

int args::123

Method hiding

we can hide main method like below.

Example:


public class Parent {

public static void main(String[] args) {
        System.out.println("In Parent main");
    }

}

class Child extends Parent {

    public static void main(String[] args) {
        System.out.println("In Child main");
    }
}

By running the Child class we can get the bellow output.

In Child main

Happy Learning 🙂

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

Java Swing JTree Example

In this tutorial, we are going to implement Swing JTree. The JTree is a class which …

One comment

  1. chagantirajesh.cultuzz@gmail.com'
    rajesh chaganti

    Can you please explain about why main method declared as static as little eloborate….. please…..

Leave a Reply

Your email address will not be published. Required fields are marked *