Table of Contents
If you have worked with Java programs before, you know that all Java programs start by running the main()
method(public static void main(String[] args)). The main()
method is the entry point.
The signature of the main method is fixed, and each portion of the statement has significance.
Why is the main method so important?
The Java compiler or JVM looks for the main method when it starts executing a Java program. The signature of the main method needs to be in a specific way for the JVM to recognize that method as its entry point. If we change the signature of the method, the program compiles but does not execute.
The execution of Java program, the java.exe is called. The Java.exe inturn makes Java Native Interface or JNI calls, and they load the JVM. The java.exe parses the command line, generates a new String array, and invokes the main() method. A daemon thread is attached to the main method, and this thread gets destroyed only when the Java program stops execution.
Syntax
There are mainly three ways in which we can write the main method in Java. They are as follows:
1 2 3 4 5 |
public static void main(String[] args) public static void main(String... args) static public void main(String args[]) |
Besides these keywords, we can also attach the final, synchronized, and strictfp keywords in the main method.
Explanation of the Keywords
The keywords in the method: public static void main(String args[]) are as follows:
public
: Public is an access specifier. Marking a method as public makes it visible to all methods across all packages. We need to mark the main() method as public otherwise, it is not visible to the JVM.
static
: The JVM invokes the main method without creating objects and hence the main method needs to be marked static.
void
: Since the main method does not need to return anything, the main method is marked void. If we define a return type for the main method, the program execution fails.
main()
: This is the default signature as defined by the JVM.
String args[]
: These are arguments passed to the main method so that users can give inputs while running the program in the form of an array of Strings.
Examples of Java main method Programs
Simple Java Program
The first example we see is the correct way to run the main method.
1 2 3 4 5 6 7 8 |
public class SimpleProgram{ public static void main(String[] args) { System.out.println("Hello World!!"); } } |
The Output of this program is
Passing arguments in the main method
If we pass arguments in the main method, we can use them as follows:
1 2 3 4 5 6 7 8 |
public class SimpleProgram{ public static void main(String[] args) { System.out.println("Hello Dear " + args[0]); } } |
Where args
is the array we pass as arguments to the main method.
Non-Public main method.
If we declare the main method as private, the program compiles but running the program results in an exception.
1 2 3 4 5 |
private static void main(String[] args) { System.out.println("Hello Dear " + args[0]); } |
Output
Defining a return value for the main method
If we define the main method to have a return value then the Java program will compile but will result in an exception as follows:
1 2 3 4 5 6 |
public static int main(String[] args) { System.out.println("Hello Dear " + args[0]); return 0; } |
Output
Overloading the main method
We have overloaded the main method to have an Integer array and a Character array in this example. We have also included the original definition of the main method. These definitions are allowed and valid, and so the code compiles. However, if we observe when the code runs, only the original definition of main i.e. public static void main(String[] args) executes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class SimpleProgram { public static void main(Integer args) { System.out.println("Overloaded method 1 with Integer args"); } public static void main(char args) { System.out.println("Overloaded method 1 with char args"); } // Original main() method public static void main(String[] args) { System.out.println("Main as it should be"); } } |
Output
Questions
Can we run java without main method?
Answer: You can not run main method without main method Java 8 onwards.
Here are the steps performed by jvm.
- JVM will load class.
- It will execute static blocks of the class.
- It will search for main method.
If static blocks are present in the class, then they will be executed even though there is no main method in the class.
Here is an example:
1 2 3 4 5 6 7 8 9 10 |
package org.arpit.java2blog.entry; public class JavaHelloWorldWithoutMain { static { System.out.println("In staic block"); } } |
When you will try to run this java program using java JavaHelloWorldWithoutMain
, you will getbelow output:
java.lang.NoSuchMethodError: main
Can we have more than one main method in class
Yes, you can have multiple main method in the class, but when you execute java program, only method with signature public static void main(String[] args)
will be called.
Conclusion
In the above article, we saw the public static void main(String[] args)- main method in Java and all its variants.