Why main() in java is declared as public static void main?

In java main() method is special method, it is entry point for launching core java application not like servlet and other containers. In Core java application we run the application then JVM search for public static void main(String args[]) method in that class and if it doesn’t find that method it throws error NoSuchMethodError:main and terminates. It is similar process required to other language like C and C++ but the method definition slightly different from java it doesn’t return any value like in C it returns int.

There are following two valid signature of main method in java as below.

  • public static void main(String args[])
  • public static void main(String… args) (As of Java 1.5)

Popular Spring Tutorials

  1. Spring Tutorial
  2. Spring MVC Web Tutorial
  3. Spring Boot Tutorial
  4. Spring JDBC Tutorial
  5. Spring AOP Tutorial
  6. Spring Security Tutorial

Why Java force the developer to define public static void main method?

Why public? In Java public is access modifier and it using when we want to method from any where like any native library JNDI, etc. So that is reason main method is public, it can be accessible everywhere and to every object which may desire to use it for launching the application.

Why static? In Java static is a keyword and it tells the compiler that particular entity belongs to a class and should be loaded once the JVM starts. So static things we could invoke without creating instance of that class. Lets suppose we do not have main method as static. Now, to call any method you need an instance of it. Now how to create instance of class which have main method and which one should be used and from where the parameters for overloaded constructors will come.

Why void? In Java void use before the method definition its mean this method is not returning any to the caller of method. But main method is invoked by JVM so there is no use of returning any value to JVM. The only thing application would like to communicate to invoking process normal or abnormal termination. This is already possible using System.exit(int). A non-zero value means abnormal termination otherwise everything was fine.

Why String[] args? it means we create a string type array. It is useful when we pass the value through command line while execute the program.

Why name is main? it is the special user define function. Although it is user define but We can’t change the name.

Conclusion
There are following conclusion we get.

  • The main method must be declared public, static and void in Java otherwise JVM will not able to run Java program.
  • Main method is entry point of core java application.
  • Main mthod is invoked by main thread in JVM.
  • Apart from static, void and public, you can use a final, synchronized and strictfp modifier in the signature of the main method in Java.
  • You could overload the main method in java as other method loading but JVM invoke only specific signature method as listed above.
  • static initializer call before JVM call main method.
  • You can use throws clause in the signature of the main method.
Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago