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.


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.

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:

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.

Java main method output 5


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.


In the above article, we saw the public static void main(String[] args)- main method in Java and all its variants.

Related Posts

  • Check if Object is null Java
    14 March

    Check if Object Is Null in Java

    Table of ContentsComparison Operator to Check if Object Is Null in JavaisNull() Method to Check if Object Is Null in JavanonNull() Method to Check if Object Is Null in JavarequireNonNull() Method to Check if Object Is Null in JavaConclusion An object in Java is an instance of a class. It is a real entity existing […]

  • How to print multiple variables in java
    31 January

    How to Print Multiple Variables in Java

    Table of ContentsWays to Print Multiple Variables in JavaUsing System.out.printUsing System.out.printfPrint Multiple Variables in Java Using LoggerFrequently Asked QuestionsHow Do I Print Multiple Values on One Line in JavaHow to Print Multiple Integers in JavaHow to Print String and Integer in Same Line in Java In this post, we will see how to print multiple […]

  • What is == in java
    01 December

    What is == in java

    Table of ContentsWhat is meaning of == in JavaCode example of ==Object equality using equal to ==The difference between == and equals== in EnumConclusion In this tutorial, we will learn what is == in Java as it is a common lexeme that we will use while developing applications. The == lexeme can be confused with […]

  • Multiple classes in one file in Java
    18 October

    Multiple classes in one file in Java

    Table of ContentsIntroductionMethods to Implement Multiple Classes In One Java Program1) Nested classes2) Multiple non-static nested classes In this post, we will see how to have multiple classes in one file in java. Introduction You need to have any number of classes in a single Java file, but there is a restriction that you can […]

  • Break out of nested loop in java
    22 July

    How to break out of nested loops in Java

    Table of ContentsUsing break (will break inner loop)Using named loopUsing named blockUsing returnConclusion In this post, we will see how to break out of nested loops in Java. Using break (will break inner loop) It is very important to understand how nested loops work to ensure that applying break will output the desired result. If […]

  • Java projects for beginners
    30 April

    Top 20 Java Projects for Beginners

    This tutorial provides the top 20 java projects for beginners for practice. These projects will help you to learn java quickly and efficiently.

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.