public static void main(String[] args) – Java main method

public static void main(String args[]) - Java main method

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:

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.

The Output of this program is
Java main method output 1

Passing arguments in the main method

If we pass arguments in the main method, we can use them as follows:

Where args is the array we pass as arguments to the main method.
Java main method output 2

Non-Public main method.

If we declare the main method as private, the program compiles but running the program results in an exception.

Output
Java main method output 3

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:

Output
Java main method output 3

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.

Output
Java main method output 5

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.

  1. JVM will load class.
  2. It will execute static blocks of the class.
  3. 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:

When you will try to run this java program using java JavaHelloWorldWithoutMain, you will getbelow output:

In staic block
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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *