Java Main Method

Generally, the Java main method is “public static void main(String[] args).” It is the starting point of the Java program from where JVM starts the execution of the Java program. If we did not specify the main() in a java program, the JVM will not execute the program, and will throw the error like this:

The basic syntax for the Java main method is as follows:

Syntax:

main(String args[]) 

But it is used with two keywords, public and static, so the complete syntax is as follows:

public static void main(String[] args)

Before diving in-depth with the main() method, let’s look at the quick index of the topic:

What is the main method in Java?

The main() is the first method that we may encounter in Java. JVM starts executing the Java program from the main method. As we know, a method is a block of code that can be executed as a single operation. So, the main() will search for the instructions such as objects, classes, methods and sequentially execute them. The main() always takes an array of String objects.

Different ways to write the main()

The syntax for writing the main() may have a little bit different. Because it can be written in various formats. All of these syntaxes are a valid way of writing the Main(). Let’ see some commonly used ways to write the main():

public static void main(String []args) { }
public static void main(String args[]) { }
public static void main(String...args) { }

Apart from all the above syntaxes, we can add the “strictfp” keyword with the main(). The strictfp keyword is used for compatibility between processors when working with floating-point values:

public strictfp static void main(String[] args) { }

The synchronized and final can also be used with the main method, but they don’t have a significance here.

Though, the final keyword can be used on args to finalize the array.

public static void main(final String[] args) { }

For a random, we can use all of the above keywords with the main(), which is, of course, meaningless.

final static synchronized strictfp void main(final String[] args) { }

public static void main( String[] args)

Let’s understand what the meaning of each keyword incomplete main() is. The complete syntax of the Main() is “public static void main(String[] args).” We can only change the name of the String array argument.

Why is it public?

The public keyword is an access modifier that should be public so that JRE can execute this method. The non-public method is not executed due to some access restrictions. Let’s see what happens if we remove the public keyword with the main(). Execute the below code:

Compile and run the above code; it will produce the below output:

From the above output, the JVM is not able to find the main().

Why is it static?

When JRE starts the execution of the Java program, there is no object of the class available. Here comes the role of a static keyword. The main() method must be static. If it is not, JVM Would not be able to call the main() because there is no object of the class is available. Let’s see what happens without the static keyword:

Compile and run the above code; it will produce the output as follows:


From the above output, we can see the error as “the main method is not static in class Hello.”

Why is it void?

It is necessary for Java that every method provides the return type. A return type may be a primitive type such as int, float, double, a reference type, or void type(returns nothing) as the Java main() does not return anything, so its return type is void. If we return something fro the main(), it will throw a compilation error.

Consider the below code:

Compile the above code; it will throw a compilation error as follows:

What is the String args?

The main method takes a single type String array, called Java command line arguments. let’s consider the below code:

Demo.java:

public class Demo {
public static void main(String[] args){
for(String s: args)
{
System.out.println(s);
 }
}
}

execute the below commands to compile and run the above code:

javac Demo.java
java Demo 1 2 3

The above command will display the passed argument to the command.

Output:

1
2
3

main() command-line arguments in eclipse

To pass the command line argument in a java program in eclipse, right-click on the program name and navigate to Run as -> Run Configurations. You will see a window like this:

Enter the arguments and click on the Run. It will execute the program and display the arguments. Consider the below output:

Running the main()

Usually, we start a Java program form the command line (console). When we call the java or javac command, it tells the JRE that what classes and arguments have been passed to the main(). Then the main() executed inside the JVM.

The Java main class

The class containing the main() is referred to as the Java man class. If we have more than one class having the main() in our Java program, then the JVM will instruct to run one of them. However, we can call the other main() from inside the main method.

How main() works internally

The main task of the main method is to start the execution of the work. Let’s understand how it works.

In a Java project, there are multiple JNI (Java Native Interface) calls. These calls are useful in loading the DLL (JVM). A tool called JNI is used to bridge between the virtual machine and programming language. It is not possible to run the JVM without JNI.

Let’s consider the following points that demonstrate how JVM works.

  • All the Java applications are starts with its entry point that is the main() method.
  • Each statement defined in main() executes in the specified order until the last statement.
  • The static keywords allocate memory for the objects. That is why we don’t need to instantiate a class to call the method.
  • String[] args is an array of the String object, we can pass the command line arguments while running the program.
  • The public keyword defines that the method can be called by any object.

What happens if we did not specify a main()

If we did not specify the main(), the program will compile but not run. This is because the JVM will not recognize the main method. As we have discussed above, the JVM always looks for the main method to start the program’s execution. It will throw an error as follows: