What is meaning of main() method in Java ? and Why is main() method public static and void in java?

  • public – access modifier, meaning global visibility
  • static – the method can be accessed straight from the class, we don’t have to instantiate an object to have a reference and use it
  • void – means that this method doesn't return a value
  • main – the name of the method, that’s the identifier JVM looks for when executing a Java program

Why is main method public static and void in Java? Will the program compile, if the main method is not static?

Java program's main method has to be declared static because keyword static allows main to be called without creating an object of the class in which the main method is defined. If we omit static keyword before main Java program will successfully compile but it won't execute.

For a little detailed description, look at the usual signature of Java's main method

public static void main (String args[])

Above code line begins defining the main method. This is the line at which the program will start executing. All Java applications begin execution by calling main.

The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared. In this case, main must be declared as public, since it must be called by code outside of its class when the program is started. The keyword static allows main to be called without having to instantiate a particular instance of the class. Without having declared main method static, your program will successfully compile but won't execute and report error at run time. This is necessary since main is called by the Java interpreter before any objects are made. The keyword void simply tells the compiler that main does not return a value. The main is the method called when a Java application begins. Keep in mind that Java is case-sensitive. Thus, Main is different from main. It is important to understand that the Java compiler will compile classes that do not contain a main method. But the Java interpreter has no way to run these classes. So, if you had typed Main instead of main, the compiler would still compile your program. However, the Java interpreter would report an error because it would be unable to find the main method.

Hope you have enjoyed reading the reason why main in Java is declared public static and void. 

Most Important Questions related to Main() Method In Java

  Can we overload the main method in Java? Which main method JVM will call?

  Can we override the main method in Java?

  Can we make main final in Java?

  Can we make main synchronized in Java?

 

Can we overload main in Java?

Yes you can overload the main method in Java, nothing wrong with this but Java will only call your specific main method, i.e. main method with the following signature:

public static void main(String[] args) or  public static void main(String args...) which is the main method as variable argument method and only supported post-Java 5 world.

Can we override main in Java?

No, you can not override the main method in Java, Why? because main is a static method and in Java static method is bonded during compile time and you can not override static method in Java. If you declare a method with same name and signature its called method hiding.

 Can we make main final in Java?

Of course, you can make the main method final in Java. JVM has no issue with that. Unlike any final method  you can not override main in Java.

 Can we make main synchronized in Java?

Yes, main can be synchronized in Java,  synchronized modifier is allowed in the main signature and you can make your main method synchronized in Java.

Comments

Popular posts from this blog

Java Stack and Heap: Java Memory Allocation Lesson.

What is JVM, JDK and JRE in Java?

Java Memory Allocation.