Tuesday, May 24, 2016

Why main method should be public static void main(String[] args) in java?

Hello friends, Once again welcome back to Java Jagran. today i am here with "Why main method should be public static void main(String[] args) in java?" We all know that thsi is the signature/prototype of main method, but the question is why this? And the reason is here:
For understanding this we have to first understand little bit about JVM. First we have to understand how JVM calls main method of our class? When ever we are starting JVM by calling:
                          cmd> java MyClass arg1 arg2 ...       
This "java" program is nothing but our JVM. So it will collect all the three arguments( MyClass, args1, args2 ...). Then after it will create one String array and stores arg1, arg2 ... in that array. Then it will calls main() method on our class by passing that String array. Now lets understand each and every key of main method.
  • public: It must be public because if we take it as private then it must be only called from inside that class, if we take it as default then it must be only called from inside that class or same package, if we take it as protected then it must be only called from same class, same package, child classes of other package also. But JVM class is the other class that is not available in same package and also not a child class of our class. So it must be public.
  • static: It must be static because JVM is not creating object of our class for calling main method. The reason why JVM use not to create object of our class is: If our class have constructors then how JVM will identify that which constructor they have to use for creating object. And if it is parametrized then what data they have to pass in parameter. For solving these problems only sun people given it as static.

  • void: It must be void because main method is called by JVM. And if it is having some return value then what JVM will do with that data. It's a useless. That why sun people kept it as void.
  • main(): It's name must me main because if this is not specified then how JVM will know which method they have to call. That why sun people written code in JVM that will call only method with name "main".
  • String[]: As I already said, JVM use to collect all arguments and create one String array and then pass it to main method. The reason they taken it as String[] only is: In String we can store any type of data. For example if they taken it as int then how can we pass float/double and String value, If they taken it as float/double then how can we pass String, boolean etc data. But in String we can pass any type of data like int, float, boolean, String etc.

Now lets understand how  JVM calling main method: When ever we are stating JVM by calling  
                             java MyClass arg1 arg2 ...
Then JVM do like this:
                             MyClass.main(new String[]{arg1, arg2, ...});


Now lets see possible prototype of main method:
  • public static void main(String []args) 
  • static public void main(String []args)
  • public static void main(String ...args)
  • public final synchronized strictfp static void main(String[] params)

No comments:

Post a Comment