Although the return type is not a part of a method's signature, JVM looks for exact declaration as
public static void main(String[] args)
My assumption is that since method signature does not have "return type" included, I must be allowed to change the return type.
But if I change it to public static int main(String[] args)
and return a value lets say 0, JVM is not able to execute the program and exits with an error
Error: Main method must return a value of type void in class TestData, please define the main method as: public static void main(String[] args)
Why do Java Specifications want the main method to be void only? Why is this restriction, when return type is not a part of method signature?
This question is different than Why is main() in java void? : this question is closed as it is opinion based, while I am looking for the code places/ processes from where JVM is invoking this method, and what are the limitations which are forcing them to keep this method as void. What are the design decisions they have taken (and the reasoning behind), and where is it documented.
I am looking for facts, so as to know the reason why is it done so. Please don't mark it duplicate without going through the details of the question.
PS: exit, is another thing. I am more concerned about entry of the program. Also, the returned value might not be to the use of JVM, restricting it to void limits extensibility.
So far what I have learnt from this question is : Java specifications have explicitly fixed return type to avoid confusion to migrating programmers (C/CPP) who may expect this value to return to OS, but since JVM is in-between this value will never be returned to OS. For this special purpose (returning value to OS) they have provided System.exit() method.
For all those who are suggesting that return type is part of signature-- Just try to define both of the following methods in a class
public static void main(String[] a){ } public static String main(String[] a){ }
You will get a compilation error, because signature of both of these methods is same.
Java main method doesn't return anything, that's why it's return type is void . This has been done to keep things simple because once the main method is finished executing, java program terminates. So there is no point in returning anything, there is nothing that can be done for the returned object by JVM.
Why the main () method in Java is always static? Java main() method is always static, so that compiler can call it without the creation of an object or before the creation of an object of the class. In any Java program, the main() method is the starting point from where compiler starts program execution.
this won't. I mean if the class itself is not public JVM can still access main method that means there is no need of main to be public. But if we don't declare it as public, it will throw an error.
The main method is public in Java because it has to be invoked by the JVM. So, if main() is not public in Java, the JVM won't call it. That's all about why the main method is declared public and static in Java.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With