Wednesday, 18 January 2012

Why there is public static void main(String[] ar) ?


Lets look at what each of these keywords implies :-

public : main method can be accessed outside of the class TestMain
static  : main method can be accessed without creating instance of the class.
void   : main method doesn't return any value.
main   : entry point for Java Interpreter.



Now Lets look into the variant of main methods and how java respond to these :

1. Very simple main method , all valid signature
public class TestMain 
{
    public static void main(String[] args)
    {
        System.out.println("Test");
    }
}   
Result : Compiled and Run Sunccessfully






2. Java 5 Var Args method Signature

public static void main(String ...args)  {

}

Result : Compiled and Run Sunccessfully

3.Method argument name change
public class TestMain 
{
    public static void main(String[] str)
    {
        System.out.println("Test");
    }
}
Result : Compile and Run Successfully , Its not mandatory to follow same naming conventions as String[] args

4.Change in the name of main method to Main
public class TestMain 
{
    public static void Main(String[] str)
    {
        System.out.println("Test");
    }
}
Result :
Compile but Error at Run time- Exception in thread "main" java.lang.NoSuchMethodError: main
Remember Java is case sensitive.

5.Access specifies is changed from public to private
public class TestMain 
{
    private static void main(String[] str)
    {
        System.out.println("Test");
    }
}
Result: Compile but Error at Run time - Main method not public.

6.Static is removed from method signature
public class TestMain 
{
    public void main(String[] str)
    {
        System.out.println("Test");
    }
}
Result : Compile but Error at Run time- Exception in thread "main" java.lang.NoSuchMethodError: main
 
7.Return type is changed from void to int
public class TestMain 
{
    public static int main(String[] str)
    {
        System.out.println("Test");
        return 0;
    }
}
Result : Compile but Error at Run time- Exception in thread "main" java.lang.NoSuchMethodError: main

8.Method arguments are removed
public class TestMain 
{
    public static void main()
    {
        System.out.println("Test");
    }
}
Result : Compile but Error at Run time- Exception in thread "main" java.lang.NoSuchMethodError: main

9.main method and overloaded Main methods
public class TestMain 
{
    public static void main(String ar[])
    {
        System.out.println("Test main : main(entry) method");
        String str[]={"Hello"};
        TestMain.Main(ar);
        TestMain tm=new TestMain();
        tm.Main();
    }
    public static void Main(String ar[])
    {
        System.out.println("Test Main :Main(class) method");
    }
    public void Main()
    {
        System.out.println("Test Main:Main(Instance) method");
    }
}
Result :Compile and Run Successfully
Output :
Test main : main(entry) method
Test Main : Main(class) method
Test Main : Main(Instance) method
 
10. Class without main method
public class TestMain 
{
   
}
Result :Compile but Error at Run time - Exception in thread "main" java.lang.NoSuchMethodError: main

By looking into above variants its quite evident that Java wants the programmer to define one entry point (main method) with specified signatures , so that run time environment can load the class and then instantiate the Class after that.

No comments:

Post a Comment