Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is main() in java void?

Tags:

java

People also ask

Why main () method is public static and void in Java?

The main() method is required because the compiler starts executing a program from this entry point. The JVM needs to instantiate the class if the main() method is allowed to be non-static. JVM can call the static methods easily without creating an instance of the class by using the class name only.

Why Main is public in Java?

1. The main method must be declared public, static and void in Java otherwise, JVM will not able to run Java program. 2. JVM throws NoSuchMethodException:main if it doesn't find the main method of predefined signature in class which is provided to Java command.

What is main () in Java?

The main() is the starting point for JVM to start execution of a Java program. Without the main() method, JVM will not execute the program. The syntax of the main() method is: public: It is an access specifier.


The main() method must indeed have a void return type. From the Java Language Specification on "Execution - Virtual Machine Start-Up" (§12.1.4):

The method main must be declared public, static, and void. It must accept a single argument that is an array of strings.

It goes on to describe when a program exits in "Execution - Program Exit" (§12.8):

A program terminates all its activity and exits when one of two things happens:

  • All the threads that are not daemon threads terminate.
  • Some thread invokes the exit method of class Runtime or class System and the exit operation is not forbidden by the security manager.

In other words, the program may exit before or after the main method finishes; a return value from main would therefore be meaningless. If you want the program to return a status code, call one of the following methods (note that all three methods never return normally):

  • System.exit(int status) - Equivalent to Runtime.getRuntime().exit(status)
  • Runtime.exit(int status) - Terminates the currently running JVM by initiating its shutdown sequence (run all registered shutdown hooks, and uninvoked finalizers, if necessary). Once this is done the JVM halts.
  • Runtime.halt(int status) - Forcibly terminates the currently running JVM.

Of the three, System.exit() is the conventional and most convenient way to terminate the JVM.


This is an interesting discussion on velocityreviews on the same topic:

Highlight:

Incidentally, this is considered bad style in C and C++ just because it's the wrong signature for main, not for any universal reason independent of programming languages. It's one of those things that is not really supposed to work, but might on your implementation.

In Java, the reason main returns void is threads. C and C++ were both designed as languages before multithreading was a widely known technique, and both had threads grafted onto them at a later date. Java was designed from the beginning to be a multithreaded environment, and frankly, it would be unusual to write any non-trivial Java application that doesn't use more than one thread. So the idea that a program moves linearly from the beginning to the end of main is a bit outdated.

written by

www.designacourse.com The Easiest Way to Train Anyone... Anywhere. Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation


The reason for the main method having void as return type is that once main finishes, it doesn't necessarily mean that the entire program finished. If main spawns new threads, then these threads can keep program running. The return type of main doesn't make much sense at this point.

For example, this is very common in Swing applications, where the main method typically starts a GUI on the Swing thread, and then main finishes... but the program is still running.


You can return an int with System.exit().

Returning anything other than an integer doesn't make much sense, as the OS expects an integer. In case nothing is returned the default is 0, which means OK. Other values typically are used to signal errors or special conditions.