Why does Java Specifications want the main method of a program to be void only?
The main
is void is specified in JLS 12.1.4 Invoke Test.main
:
The method
main
must be declaredpublic
,static
, andvoid
.
Sometimes the answer to a why? is because the specification says so (aka Quod Ego Dixit, or Because I Say So (Terry Pratchett)). Sometimes the decisions of a language designer are not available and the only thing we can do is guess. This is also the reason the obvious duplicate (Why is main() in java void?) is closed as primarily opinion-based.
Now as the - to me - obvious reason: JLS 12.8 Program Exit says:
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 classRuntime
or classSystem
, and the exit operation is not forbidden by the security manager.
This means that the end of the main
method does not necessarily mean the end of the program. A return code from a main
method has a well-known meaning in languages like C: it is the process exit code. This meaning doesn't apply when the main
method may exit long before the process itself ends.
Most non-trivial Java applications have - usually- a short-lived main method that kicks off one or more long-living non-daemon threads. Allowing a return code from a Java main
method could imply a meaning that is not actually there: that the return value is the process exit code. So explicitly specifying that main
must have a void return type is good thing: it reduces confusion or assigning meaning that isn't there.
Why does the main function in Java reside in a class?
Probably for the same reason you put question marks at the end of a question: that's just how they decided it's done.
The main
method is the result of a convention that says "this is how the entry point's method signature should look" which doesn't exempt it from language semantics.
Java does not support methods outside of classes/interfaces and as such it has to be contained in one.
Why can't use Void as the return type for main method
You should use void
(lowercase v
) not Void
object. Void
object is not going to get autoboxing like e.g. int
/Integer
, see Java Language Specification for a list of autoboxing objects.
Void
is not a wrapper for void
, it is just an object that has a very similar name so it can be used in places where you need to specify a return type (like e.g. Callable<T>
), it is just for documentation purposes and to workaround some generic classes return types.
Second use case is in reflection (when you want to check the return value of a void
function, you will get Void.TYPE
).
Correct line is:
public static void main(String[] args)
Related Topics
How to Make a Java Class That Implements One Interface with Two Generic Types
How to Find Out the Currently Logged-In User in Spring Boot
Shared Memory Between Two Jvms
Spring Data JPA - "No Property Found for Type" Exception
Is There a Java Utility to Do a Deep Comparison of Two Objects
Is There an Equivalent of Java.Util.Regex for "Glob" Type Patterns
How to Add a New Line of Text to an Existing File in Java
Static Versus Non-Static Lock Object in Synchronized Block
Difference Between Java.Exe and Javaw.Exe
How to Do Union, Intersect, Difference and Reverse Data in Java
Drawing a Simple Line Graph in Java
Using Jaxb to Unmarshal/Marshal a List<String>
How to Resolve Unable to Load Authentication Plugin 'Caching_Sha2_Password' Issue
Can One Do a for Each Loop in Java in Reverse Order
Java Regex to Extract Text Between Tags
What Is @Joincolumn and How It Is Used in Hibernate