Why Public Static Void Main In Java

Java’s main() method is the starting point from where the JVM starts the execution of a Java program. JVM will not execute the code, if the program is missing the main method. Hence, it is one of the most important methods of Java, and having a proper understanding of it is very 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 the Java program, the java.exeis called. The Java.exe in turn 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.

Refer to more articles:  Why Isn't Valerie Bertinelli On Halloween Baking Championship

Syntax of main() Method

Syntax of the main() method is always written as:

Java main() Method syntax

Example

The most common in defining the main() method is shown in the below example.

Every word in the public static void main statement has a meaning in the JVM that is described below:

1. Public

It is an Access modifier, which specifies from where and who can access the method. Making the main() method public makes it globally available. It is made public so that JVM can invoke it from outside the class as it is not present in the current class.

If the main method is not public, it’s access is restricted.

Example:

Output

Error: Main method not found in class, please define the main method as:public static void main(String[] args)or a JavaFX application class must extend javafx.application.Application

2. Static

It is a keywordthat is when associated with a method, making it a class-related method. The main() method is static so that JVM can invoke it without instantiating the class. This also saves the unnecessary wastage of memory which would have been used by the object declared only for calling the main() method by the JVM.

If you try to run Java code where main is not static, you will get an error.

Example:

Output

Error: Main method is not static in class test, please define the main method as:public static void main(String[] args)

3. Void

It is a keyword and is used to specify that a method doesn’t return anything. As the main() method doesn’t return anything, its return type is void. As soon as the main() method terminates, the Java program terminates too. Hence, it doesn’t make any sense to return from the main() method as JVM can’t do anything with its return value of it.

Refer to more articles:  Why Is There Foam In My Hot Tub

If main method is not void, we will get an error.

Example:

Output

Error: Main method must return a value of type void in class Main, pleasedefine the main method as: public static void main(String[] args)

4. main

It is the name of the Java main method. It is the identifier that the JVM looks for as the starting point of the Java program. It’s not a keyword.

If we change the name while initiating main method, we will get an error.

Example:

Output

Error: Main method not found in class, please define the main method as:public static void main(String[] args)or a JavaFX application class must extend javafx.application.Application

5. String[] args

It stores Java command-linearguments and is an array of type java.lang.String class. Here, the name of the String array is args but it is not fixed and the user can use any name in place of it.

Example: Execution Process of String[]

Output

123

Apart from the above-mentioned signature of main, you could use public static void main(String args[]) or public static void main(String… args) to call the main function in Java. The main method is called if its formal parameter matches that of an array of Strings.

Note: Main Method is Cumpulosory in Java

Overloading main() Method in Java

Overloading the main() method is possible in Java, meaning we can create any number of main() methods in a program.

To overload the main() method in Java, we need to create the main() method with different parameters.

Example:

Output:

Running main() with no arguments

Refer to more articles:  Why Did Adolf Hitler Hate The Jews

Conclusion

Java main() method is the starting point of a Java program. It is the main body that is executed by the JVM, and without the main() method no Java program can be run without it.

Learn More: Java Tutorial

Java main() Method- FAQs

Can the main method be int? If not, why?

No, Java main method can not be int. There are two main reasons for it:

  1. JVM looks for public static void main(String[] args) when starting the program execution as it is the standard signature for entry. Using int signature would cause confusion and compatability issues while program execution.
  2. Having void signature means the main method will not return anything, but using int signature means the main function will have to return integer, which isn’t useful for JVM.

Can we execute a Java program without the main method?

No, From JDK7 main method is mandatory. The compiler will verify first, whether main() is present or not. If your program doesn’t contain the main method, then you will get an error “main method not found in the class”.

To check more about the topic refer to Is main method compulsory in Java? article.

Can we declare the main() method without String[] args?

Yes, we can declare the main() method without String[] args. Although it will generate an error message if we directly try to execute the main method inside the driver class as done in the below example.

Below is the correct method to write a program without String args[].

Output

Why is main void in Java?

The main method serves as the starting point for program execution in Java. The JVM doesn’t expect a return value from the main because there’s no mechanism for it to receive or process such a value.

Related Posts

Why Do I Keep Getting Kicked Out Of Netflix

Why Do I Keep Getting Kicked Out Of Netflix

Within a society where instant digital gratification is the norm, the abrupt halt of Netflix streaming can feel like an untimely pause in a gripping narrative.You may…

Why Does My Tooth Hurt When I Chew

Simply put, your tooth shouldn’t hurt when you bite down. If you’re experiencing tooth pain when you chew or whenever pressure is applied, then you should contact…

Why Is My Zelle Payment Processing

What should you do if you notice your payment is pending on Zelle? Whether you are new to using Zelle or have been regularly using the app,…

Why Do Dogs Lick Their Beds

Entering your room and finding your furry bundle of joy passionately licking the bed…. You may be interested Why Is Xlookup Not Working Why Duke Essay Example…

Why Bitter Taste In My Mouth

Below are some of the things that may cause a persistent bad taste in the mouth. However, this is not a comprehensive list. People can get a…

Why Am I Not Surprised

Why Am I Not Surprised

You may be interested Why Computer Science Essay Why Didn’t Voldemort Kill Harry Why Does Highbottom Hate Snow Why Does My Cat Watch Me Eat Why Do…