Main method in Java
This post will discuss about the main method in Java.
The main method in Java serves as an entry point for the application, similar to the main method in C and C++. Every Java application must contain the main method. The JVM (Java Virtual Machine) starts its execution by invoking the main method of the specified class, and main method will subsequently invoke all the other methods required by the program. The main method can contain any code that is valid in Java, such as variable declarations, assignments, expressions, statements, loops, conditional statements, method calls, etc. The code inside the main method is executed sequentially from top to bottom until it reaches the end of the method.
Syntax of main method
The main method must be declared public, static, and void. The public and static modifier can be either written as public static or static public, but the convention uses the public static. It accepts a single argument: an array of strings through which the Java runtime engine (JVM) passes information to the application via command-line argument. The main() can be written as:
|
1 2 3 |
public static void main(String[] args) { … } |
or
|
1 2 3 |
public static void main(String... args) { … } |
Let’s break down the syntax and understand what each word means:
publicis an access modifier that specifies that the main method can be accessed from anywhere, even outside the class. This is necessary because the JVM invokes the main method from outside the class.staticis a keyword that indicates that the main method belongs to the class and not to any object of the class. This means that the main method can be called without creating an instance of the class. This is also necessary because the JVM calls the main method before creating any object.voidis a keyword that specifies that the main method does not return any value. This is because the main method only starts the execution of the program and does not need to return anything.mainis the name of the method that is recognized by the JVM as the entry point of the program. The name must be exactly main, otherwise, the JVM will throw an error.String[] argsis a parameter that represents an array of strings. This array contains any command-line arguments that are passed to the program when it is executed. The string array argument can be renamed to anything, but the convention is to choose between"args"or"argv".
We can also make the main method final in Java by using the final modifier and synchronized by using the synchronized modifier in the method signature, as shown below:
|
1 2 3 |
public static final synchronized void main(String[] args) { System.out.println("Inside main"); } |
The main method can also be overloaded, which means that we can define more than one main method in a class with different parameters. However, only one main method will be considered as the entry point of the program by the JVM, and that is the one with String[] or String… as the parameter type. Note that we cannot declare both main(String…) and main(String[]) in a Java class.
Is Java program execution possible without main method?
It is possible to execute a Java program without a main method, but only in some specific cases. For managed environments (container environments) like Servlet (or EJB), the main() is not the entry point of the application as they have their own life-cycle methods like init(), service() or destroy() which is used by the container. The main method is the entry point for a Java application that runs on the Java Virtual Machine (JVM).
However, in Java 6 and before, it was possible to execute a Java program without a main by using a static initialization block. This is a block of code that is executed only once when the class is loaded by the JVM. It can contain any executable statements, including printing to the console. Here is an example of a Java program that runs without a main method using a static block:
|
1 2 3 4 5 6 7 8 |
class Main { static { System.out.println("Java Program without main()"); System.exit(0); } } |
This will output “Java Program without main()” when executed with Java 6 or lower. However, with Java 7 or higher, it will throw an error. Therefore, it is not recommended to use this technique for running Java programs without a main method.
That’s all about the main method in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)