Entry point method in Java

Kamran Babayev
3 min readJun 17, 2023

--

In Java, the entry point method is the starting point for the execution of a Java program. The entry point method is a special method that is required in every Java application, and it is the method that the Java Virtual Machine (JVM) calls to begin the execution of the program.

The entry point method in Java is defined as:

public static void main(String[] args) {
// Code goes here
}

Here’s an explanation of the different components of the entry point method:

  • public: It specifies that the entry point method is accessible from outside the class.
  • static: It indicates that the entry point method belongs to the class itself rather than an instance of the class.
  • void: It specifies that the entry point method does not return any value.
  • main: It is the name of the method. This name is recognized by the JVM as the entry point.
  • String[] args: It is the parameter passed to the entry point method. It represents the command-line arguments passed to the program.

When you run a Java program, the JVM looks for the main method with the exact signature (public static void main(String[] args)) and executes the code inside it. The args parameter allows you to access any command-line arguments provided when the program is launched.

It’s worth noting that the main method serves as an entry point for standalone Java applications. In other scenarios, such as Java applets or servlets, different entry points are used.

Why do we need to declare main method as static in Java?
The main method is declared as static. It is called by JVM when we run a class. The JVM does not know how to create an object of a class. It needs a standard way to start the execution of a program.
Therefore, the main method is declared as static so that the JVM can call it using the class name which is passed on the command line. If we do not declare the main method as static, it will be considered as an instance method. The code will be compiled successfully without generating any error message. But at runtime, the code will generate an exception named: “NoSuchmethodError: main”.

Key Points:

1. public static void main(String args[ ]) is a line at which the program will start executing.

2. main() method in java is always called by JVM (Java Virtual Machine) before any objects are created.

3. It does not return any value.

4. Since Java is case-sensitive, Main is different from main. If you type Main instead of main, java compiler would still compile your program but java will report an error because it would not find main() method.

5. If a program does not contain the main method in a class, Java compiler will compile it but cannot run it.

6. Java main() method can be overloaded but cannot override it.

--

--

Kamran Babayev

Willing to learn new technologes, improve technical skills and apply them in software development.