Q. Why main function is defined as public and static?
Ans:- Public access specifier is specified so that the program wud be visible outside the class ,Hence main function is declared as public since it may be called by code outside its class when the program is started . the Keyword static allows main function to be called without having to intiate the instances of class and as since static function are called first that y main is declared first .
Q: What if the main() method is declared as private?
Ans : The program compiles properly but at runtime it will give "main() method not public." message.
Q: What if the static modifier is removed from the signature of the main() method?
Ans: Program compiles. But at runtime throws an error " NoSuchMethodError”.
Q: What if I do not provide the String array as the argument to the method?
Program compiles but throws a runtime error "NoSuchMethodError".
Q: Can an application have multiple classes having main() method?
Ans:Yes it is possible. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned.
Hence there is not conflict amongst the multiple classes having main() method.
Q:Can I have multiple main() methods in the same class?
No the program fails to compile. The compiler says that the main() method is already defined in the class.
Ans:- Public access specifier is specified so that the program wud be visible outside the class ,Hence main function is declared as public since it may be called by code outside its class when the program is started . the Keyword static allows main function to be called without having to intiate the instances of class and as since static function are called first that y main is declared first .
Q: What if the main() method is declared as private?
Ans : The program compiles properly but at runtime it will give "main() method not public." message.
Q: What if the static modifier is removed from the signature of the main() method?
Ans: Program compiles. But at runtime throws an error " NoSuchMethodError”.
Q: What if I do not provide the String array as the argument to the method?
Program compiles but throws a runtime error "NoSuchMethodError".
Q: Can an application have multiple classes having main() method?
Ans:Yes it is possible. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned.
Hence there is not conflict amongst the multiple classes having main() method.
Q:Can I have multiple main() methods in the same class?
No the program fails to compile. The compiler says that the main() method is already defined in the class.
No comments:
Post a Comment