Explain about public static void main(String args[ ])

Let us consider the following class Test
class Test
{
   // code
}

The java compiler compiles this class successfully irrespective of main() method existence.
But at run-time, if JVM is unable to find the main() method, then we will get run-time exception saying: NoSuchMehtodException:main

JVM always searches for the main method with the following prototype:
public static void main(String args[])

But, there are many questions related to this. Like, why jvm only search for this prototype? what is the use to have this prototype?
JVM is also a software program. As it is programmed in such a way in that whenever jvm starts executing a java program, it should starts its execution from the main method with prototype
public static void main(String args[]).
 
main method prototype 
 
The syntax of main method is very strict. If we do any change in the prototype, we will not get any error in the compile time, but we will get run-time exception saying that NoSuchMethodException:main.
But, the following statements are true.
  • The order of the modifiers is not important that is, instead of "public static", we can write as "static public".
  • we can declare "String[]" in any acceptable form. i.e., all the following declarations are acceptable.
main(String[] args)
main(String args[])
main(String []args)
  • Instead of "args ", we can take any valid java identifier. 
  • We can replace String[] with var arg parameter. i.e.,the following declaration is possible.
main(String[] args) => main(String... args)
  • We can declare main() method with the following modifiers also: final, Synchronized, Strictfp

  Overloading of main(): 

Overloading of main() method is possible but JVM always call the main method with String[] argument only. 
Example: 

public class TestMainOverloading {

    public static void main(String[] args)
    {
        System.out.println("main method with String[] args");
    }
  
    public static void main(int args)
    {
        System.out.println("main method with int args");
    }
}
Output:
main method with String[] args
The other overloaded method should be called explicitly as a normal method as shown below.
 Example:
public class TestMainOverloading {

    public static void main(String[] args)
    {
        System.out.println("main method with String[] args");
        main(5);
   
}
   
    public static void main(int args)
    {
        System.out.println("main method with int args");
    }
}

Output:
main method with String[] args
main method with int args

Inheriting of main():

main method can also be overridden. That is if a child class extends a super class having main method, and if we run the child class, then the execution starts from the main method in the super class.
Example:
public class TestMainOverriding {

    TestMainOverloading obj = new TestMainOverloading();
    public static void main(String[] args)
    {
        System.out.println("main method of parent class");
    }
}
class ChildTestMainOverriding extends TestMainOverriding
{
     public static void main(String[] args)
    {
        System.out.println("main method of child class");
    }
}
Output:
 main method of parent class
The class ChildTestMainOverriding extends the class TestMainOverriding which contains the main() method.Hence when we run the class ChildTestMainOverriding , the main method in the parent class will be called by JVM and execution starts from the main() method of parent class.
Hence inheritance is applicable for main() method also.

Method Hiding:

If the child class contains main() method, the main() method of parent class will be hidden by the child class main() method and when we run the child class, the child class main() method will be called by JVM. This concept is called method hiding .
Example:
public class TestMainOverriding {

    TestMainOverloading obj = new TestMainOverloading();
    public static void main(String[] args)
    {
        System.out.println("main method of parent class");
    }
}
class ChildTestMainOverriding extends TestMainOverriding
{
     public static void main(String[] args)
    {
        System.out.println("main method of child class");
    }
}
Output:
 main method of child class

 

No comments:

Post a Comment