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>
Public static void main(String [] ar)
To understand this we need to know the syntax of method and the array.
Syntax of method is :
return type methodName
so the main method is written along with void which is return type.
Syntax of Array:
datatype [] arrayName
the square braces indicates whether it is the of dimension array .Since we have one pair of square braces it is one dimension array.
The meaning of words in the main method:
Public: Public is the access specifier it is intended for the purpose of the JVM to execute the main method from any location.
Static : Static is a modifier.The main method must be declared as static so that the JVM can access the main method directly by using the class name.
When we execute a java program we use the class name so when we write static it will help the JVM to access the main method.
If we remove static then it becomes instance method,to access an instance method we should create and object and then invoke the method using the object reference.
void : Void is the return type of main method. The Caller of the main method is JVM and the JVM does not expect any value from the main method and there fore the main method should not return any value.This is the reason for specifying Void.
main : Main is the method name and it is fixed as per the Java coding conventions.
String[]: It is used for storing the command line arguments.The name of the array can be any valid Java identifier.
So after String[] we can give name as any valid java identifier it can be 'ar' or it can be 'args'.