Saturday, August 18, 2012

What is main method in Java ? Why main is public static and void

What is main method in Java
main method in Java is an standard method which is used by JVM to start execution of any Java program. main method is referred as entry point of Java application which is true in case of core java application but in case of container managed environment like Servlet, EJB or MIDlet this is not true as these Java programs have there own life-cycle methods like init(), service() or destory() for Servlet's which is used by container. main method in Java is run by main thread which is a non daemon thread and Java program runs until main method finishes or any other user thread is running. When we start JVM by running java command we also provide name of class which contains main method, which is later invoked by JVM to start Java program execution. for example in below command :


C:\Documents and Settings\JavaTutorial>edit Calculator.java


code of Calculator class
public class Calculator{
   public static void main(String args[]){
        System.out.println("I am main class which contains main method");
   }
}

C:\DOCUME~1\JavaTutorial>javac Calculator.java

C:\DOCUME~1\JavaTutorial>java Calculator
I am main class which contains main method

Calculator class must contain public static void main(String args[]) method. Now if we change the signature of main method and try to run same Java program we will get Error as shown below :

public class Calculator{
   public static void main(String args){
        System.out.println("I am main class which contains main method");
   }
}

C:\DOCUME~1\sharma>javac Calculator.java

C:\DOCUME~1\sharma>java Calculator
Exception in thread "main" java.lang.NoSuchMethodError: main

 because we have changed String[] to String which means JVM not able to find standard main method which is required to start execution of Java program


Valid Signature of main method in Java 

main method is a standard method and has pre specified signature, if you change the signature of main method JVM will not be able to locate main method will throw Exception at runtime as shown in above example. main method is public, static and void and accept an String[] as argument and from Java 5 onwards it can also accept variable arguments instead of array. following signatures are valid main method signature in Java :

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

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

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

You can also use certain modifier like final, synchronized and strictfp along with main method in Java.



Why main method is public static and void in Java
What is main method in Java and why main is static in Java
This is very famous core Java interview question, and appeared on many fresher and experience level interviews. Though ever java programmer uses main method not every one is familiar with reason why main is static or why main is public in Java. Here are few reasons which make sense to me to think of Why main is public, static and void in java

main method in Java is public so that its visible to every other class, even which are not part of its package. if its not public JVM classes might not able to access it.

main method is static in Java, so that it can be called without creating any instance. While JVM tries to execute Java program it doesn't know how to create instance of  main class as there is no standard constructor is defined for main class.

main method is void in Java because it doesn't return any thing to caller which is JVM .


Difference between C,C++ and Java main method
If you come from C and C++ programming language than you know what is main method as both of these program also uses main() as entry point for program execution but main method in C and C++ is quite different than main method in Java. Main method in java doesn't return anything and has return type void while main method in C and ++ return int.


That's all on What is main method in Java, What are valid signature of main method in Java, What happens if you put incorrect signature of main method, Why main method is public, static, and void in Java and finally we saw difference between C, C++ and Java main methods. This is one of the most important fundamental of Java programming language and every Java programmer should be familiar with it.

No comments:

Post a Comment

Java67 Headline Animator