+ 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??
4 ответов
+ 6
Main method mustn't return a value. It is invoked by jre and jre can't handle a return value.
+ 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)
+ 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.
0
such that it's doesn't need to return any thing
....