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.

Related Posts

  • 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.

  • How to end program in java
    08 March

    How to end program in java

    Table of ContentsUsing System.exit() to end java programExample to demonstrate System.exit() methodHow to end java program in an if statement in javaUsing return to end current method Ho In this post, we will see how to end program in java. You can use System.exit(0) to end program in java. Using System.exit() to end java program […]

  • 25 September

    Reference Variable in Java

    Table of ContentsReference variableStatic Reference VariableInstance Reference VariableLocal Reference Variable In this post, we will see about Reference variable in java. Reference variable A variable that holds reference of an object is called a reference variable. Variable is a name that is used to hold a value of any type during program execution. If the […]

  • 26 March

    Global variables in java

    In this post, we will see how to define global variables in java. Unlike C/C++, there are no standard global variables in java but you can still define global variables that can be used across all classes. Global variables are those variables that can be accessed across all the classes. Java does not support global […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.