Thursday, March 12, 2009

Why do we require public static void main(String args[]) method in Java

Question :Why do we require public static void main(String args[]) method in Java
programme? (CoreJava)
Answer :Following are few reasons why there is public static void main(String
args[])
a. public: The method can be accessed outside the class / package
b. static: You need not have an instance of the class to access the method
c. void: Your application need not return a value, as the JVM launcher
would return the value when it exits
d. main(): This is the entry point for the application
If the main() was not static, you would require an instance of the class in
order to execute the method.
If this is the case, what would create the instance of the class? What if
your class did not have a public constructor?
java Test
would get converted to Test.main() there by invoking the main()

No comments: