Why public static void main(String args[])
In most of the java interviews either for freshers or experience guys frequently facing this question. This will be easy when you have full idea about access specifiers and modifiers in java. Lets discuss about our mantra
Note: If JVM is unable to find main( ) then we will get a Runtime Exception saying : Exception in thread "main" java.lang.NoSuchMethodError: main
Here we can observe that's why the main( ) prototype must be public static void main(String args[]) Let's see .
public: JVM calls main( ) from anywhere, so it should be public
static: without creating object also JVM has to call this method.
void: here main( ) won't return anything to JVM
main( ): this is the method which has configured already in the JVM
String[] args: commandline- arguments in the form of array of instances of the class String
In this we can have several possibility changes to form main method
Change 1: The Order of the modifiers is not important, i.e instead of public static ,we can use static public also.
Change 2: We can declare String array in any acceptable form i.e
Change 3: Instead of 'args' we should take any java valid identifier
public static void main(String args[]).
What is the Importance of the public static void main(String args[ ])
Whether a class contains main( ) or not and whether the main( ) is declared based on requirement, these all are things are won't be checked by compiler,these things are checked by JVM at Runtime.Note: If JVM is unable to find main( ) then we will get a Runtime Exception saying : Exception in thread "main" java.lang.NoSuchMethodError: main
Here we can observe that's why the main( ) prototype must be public static void main(String args[]) Let's see .
public: JVM calls main( ) from anywhere, so it should be public
static: without creating object also JVM has to call this method.
void: here main( ) won't return anything to JVM
main( ): this is the method which has configured already in the JVM
String[] args: commandline- arguments in the form of array of instances of the class String
In this we can have several possibility changes to form main method
Change 1: The Order of the modifiers is not important, i.e instead of public static ,we can use static public also.
Change 2: We can declare String array in any acceptable form i.e
(String[] args) (String []args) (String args[])
public class Test { public static void main(String ramu[]) { System.out.println("Hello Test"); } }the above code will execute without error.
Change 4: We can replace 'String[] args' with 'var-args' parameter. i.e
main(String[] args) to main(String...args)
public class Test { public static void main(String...args) { System.out.println("Hello Test"); } }Here the arguments we can pass three dots as shown above.
Change 5: We can declare main( ) with the following modifiers also
- final
- synchronized
- strictfp
The following program consits of all above changes.
public class Test { final synchronized strictfp static public void main(String... args) { System.out.println("This is acceptable main method"); } }This class will print the value without any error.
Here some more information about main method
Case 1: Overloading of the main( ) is possible,But JVM always call (String[] args) of main( ) only see below Example.
public class Test { public static void main(String[] args) { System.out.println("String[]"); } public static void main(int[] args) { System.out.println("int[]"); } }
Output: String[]
Here we can say that above method is Overloaded. Method names are same with different signatures.
Case 2: Inheritance is possible/applicable on main( ),while executing child class ,if child class doesn't contains main( ),then parent main( ) will be executed.
public class Test { public static void main(String args[]) { System.out.println("iam from main()"); } } class Demo extends Test { }
w.r.to{java test} parent class: Output: iam from main( )
w.r.to {java Demo}child class: Output: iam from main( )
Case 3: It seems Overriding concept is applicable for main( ),but it is not Overriding it is method hiding.
class Test { public static void main(String args[]) { System.out.println("iam from Test"); } } class Demo extends Test { public static void main(String[] args) { System.out.println("iam from Demo"); } }
w.r.to Parent: Output: iam from Test
w.r.to Child: Output: iam from Demo
Note: Overloading and inheritence is applicable for main method. But overriding concept is not applicable,instead of overriding concept method hiding concept is applicable.
Interview Question on main method is given below take a look on it. Read more interview questions related.
Which of the following are valid main( ) declarations as shown bellow?
1) public static void main(String args[])
[invalid because in main the letter m is Capital]
2) public static int main(String[] args )
[invalid because the main method is return int value]
3) public static void main(string args))
[invalid because the String arr[] is not declared]
4) public final synchronized strictfp void main(String...args)
[invalid because static is missing]
5) public static void main(String...args)
[Valid]
6) public static final synchronized strictfp void main(String...args)