Why does java main method uses void?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Why does java main method uses void??

I know that void is only used when the function is not returning anything. In java, they use void in: public static void main(String[] args) and I'm a bit confused about that. Does it mean main cannot return anything??

17th Feb 2017, 5:59 PM
Baba Yunus Abdul Yekin
Baba Yunus Abdul Yekin - avatar
4 Answers
+ 6
Main method mustn't return a value. It is invoked by jre and jre can't handle a return value.
17th Feb 2017, 6:23 PM
Tashi N
Tashi N - avatar
+ 3
Main is one of the method of your program from which your program execution begins. Main method can call other methods and those methods can return something(integer, character, string, float or any other such) back to main method. But who can call main method? No one. So it doesn't return anything to anyone(any method)
17th Feb 2017, 6:12 PM
Kommoju Sunil Kumar
Kommoju Sunil Kumar - avatar
+ 3
Any method can notify the outside world by using return value or by throwing exceptions. main() is designed with void, but it can throw exception. Though the main method is the entry point of the main class activated, it can also be called by other methods, that means you can notify the caller method by throwing exception in your main method. For example, if you have a class named Foo and Foo2: public class Foo { public static void main(String[] args) { System.out.println("Hello!"); } } public class Foo2 { public static void main(String[] args) { Foo.main(null); } } Now you can run Foo2 by "javac Foo2", which will produce "Hello!". Yes, the main method can be called by other method statically.
18th Feb 2017, 3:45 AM
Jian-hua Yeh
Jian-hua Yeh - avatar
0
such that it's doesn't need to return any thing ....
17th Feb 2017, 8:55 PM
SUMANTH VENKATA SAI KRISHNA Manduru
SUMANTH VENKATA SAI KRISHNA Manduru - avatar