I was recently asked in an exam if public static void main(String arg[]) format of main method was fixed?
Can we change it?
Can we use main without any of public, static or void?
If not, why is it not hard coded that main(String arg[]) would stand for public static void main(String arg[]) always?
I was recently asked in an exam if public static void main(String arg[]) format
Share
The signature of the main method is specified in the Java Language Specifications section 12.1.4 and clearly states:
publicotherwise it would not be possible to call itstaticsince you have no way to instantiate an object before calling itStringarguments is there to allow to pass parameters when executing a Java program from the command line. It would have been possible to define it without arguments but is more practical like that (and similar to other languages)voidsince it does not make sense to have anything else: a Java program can terminate before reaching the end of the main method (e.g., by callingSystem.exit())The method signature can therefore be:
note that the varargs version (
...) is only valid from Java 5As the Java language allows the brackets
[]to be positioned after the type or the variable (the first is generally preferred),is also valid