Why main() method in java has public static void main ?
Public - main method is called by JVM to run the method which is outside the scope of project therefore the access specifier has to be public to permit call from anywhere outside the application static - When the JVM makes are call to the main method there is not object existing for the class being called therefore it has to have static method to allow invocation from class. void - Java is platform independent language therefore if it will return some value then the value may mean different to different platforms so unlike C it can not assume a behavior of returning value to the operating system. If main method is declared as private then - Program will compile properly but at run-time it will give "Main method not public." error.
The only difference between experienced and inexperienced software developers is that the experienced ones realize when they're making a mistake.
Can you think of a questions which is not part of this post? Please don't forget to share it with me in comments section & I will try to include it in the list.
No comments:
Post a Comment