Java main() method analysis – why it is public static void main(String[] args) ?

Java main method - why it is public static void main(String[] args)

Java main() method analysis – why it is public static void main(String[] args) ?

In java, we all know program execution is start from the main method, but java main method – why it is public static void main(String[] args) may be a chance that we don’t know why main method is public and static as well why it’s return type is void and about it’s argument. So in this post we did analysis of java main() method. And we will also discuss polymorphism and inheritance concept is applicable or not to the java main() method. Let’s see.

Java main method - why it is public static void main(String[] args)

First we write  a simple java program which contains main() method. And then we Java main method – why it is public static void main(String[] args)

class HelloWorld
{
   public static void main(String[] args)
   {
      System.out.println("Hello World!");
   }
}

Download First Java Hello World Program, click here.

Java main method – why it is public static void main(String[] args)

public :

public is a keyword modifier in java. If we declare a method with public modifier then we can access that method from outside of the class. And if we declare a method non-public then there is some access restriction is applied and we can’t access that method. So if we did not declare main() method as public then at the run time execution JVM won’t be access main() method and get the error. That’s why main method has to be public.

static :

static is also a keyword modifier in java which is applicable only for the methods and variable not for the class. If we declare method or variable as static then it produce only single copy for entire class and don’t need object to access that method or variable. At the java program runtime starts there is no object at that present class. That’s why main() method is static and at the runtime JVM can load the class in memory and call main() method.

void :

Java programming mandates that every method signature provide the return type. If method doesn’t return anything then declare that method also as void. Same Java main() method doesn’t return anything so we need to declare as void. Once main method finish executing java program terminates. And we don’t need to return anything to JVM. If we return something from the main() method then we will get compile time error saying unexpected return value.

main :

This is the name of the main() method. It is set internally by designer of Java language. If we make mistake to write main or try another then JVM won’t be find main() method and produce an error saying main method is not found.  We can change the name of main() method to any customized name if we know the internal architecture of the JVM. But it is not require as a programmer level.

Sting[] args :

Java main() method can accept a single argument of type String array. This is called java command line arguments. Where args is name of argument and we can set any custom name there. An this argument is only String type because mostly used data type in java is String type and also we can easily convert String data type to any other data type.

 

Note:

In java order of modifier doesn’t matter so we can write main() method as below.

public static void main(String[] args)
 or
static public void main(String[] args)

above both statements are equal and valid.

That’s all about Java main method – why it is public static void main(String[] args) and I hope now all your doubts has been clear like why it’s public, static,etc.

Thank You ! 🙂

Stay Connected with Place4Java and follow us on our Facebook Page.

Be the first to comment

Leave a Reply

Your email address will not be published.


*