Home > DeveloperSection > Beginner > Quick overview of “public static void main(String args[])”

Quick overview of “public static void main(String args[])”


Java Java 
Ratings:
0 Comment(s)
 906  View(s)
Rate this:

Quick overview of “public static void main(String args[])”

 

·         What if the main method of a class is declared as private?


If the main() is declared as private, the program compiles properly but at runtime it will give "Main method not public" message.

 

 

·         What if the static modifier is removed from the syntax of the main method?

In this case the program will compile. But at runtime it will throw an error "NoSuchMethodError".

 

 

·         What if I write static public void instead of public static void?

 

Most of you will think that it will throw an error but it’s not like that. The program compiles and runs properly without throwing any syntax error.

 

 

·         What if I do not provide the String array as the argument to the method?

 

Don’t ever do this as it will throw an error. The program will compile but it throws a runtime error "NoSuchMethodError".

 

 

·         What is the first argument of the String array in main method?

 

The String array is empty. It does not have any element. This is unlike C/C++ where the first element by default is the program name.

 

 

·         If I do not provide any arguments on the command line, then the String array of Main method will be empty or null?

 

It will be empty. But not null.

 

 

·         How can one prove that the array is not null but empty using one line of code?

 

Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.

 

·         Can an application have multiple classes having main method?

 

Yes it is possible to have multiple classes having main method. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned. Hence there is no conflict amongst the multiple classes having main method.

 

 

·         Can I have multiple main methods in the same class?

 

No the program fails to compile if you have multiple main() methods within a single class. The compiler says that the “main method is already defined” in the class.

 

 

 

·         What if main() method is not static in Java, will it throw an exception or an error?

 

Java program's main method has to be declared static because keyword static allows main to be called without creating an object of the class in which the main method is defined. If we omit static keyword before main Java program will successfully compile but it won't execute.

It will throw an error:

Error: Main method is not static in class XYZ, please define the main method as:
public static void main(
String[] args)

 

 

Some useful points to keep in mind while programming: (for beginners)

 

1)      If you define two or multiple main() methods within a single class, it will throw an error.

Example: class name: dev; file name: dev.java; total number of main methods 3.

class dev

{

public static void main(String args[])

{

System.out.println("4");

}

public static void main(String args1[])

{

System.out.println("5");

}

public static void main(String args2[])

{

System.out.println("6");

}

}

 

 

 

 

 

2)       If you define multiple main methods but with different return type, then to it will throw an error.

Example: class name: dev1; file name: dev1.java; total number of main methods 2.

 

class dev1

{

public static void main(String args[])

{

System.out.println("4");

}

public static int main(String args1[])

{

System.out.println("5");

}

}

 

 

 

 

3)      If you define multiple main methods with different return type, but the second main() does not have any parameter passed into it, then it will compile and execute.

Example: class name: dev2; file name: dev2.java; total number of main methods 2.

 

 

class dev2

{

public static void main(String args[])

{

System.out.println("4");

}

public static int main()

{

return(5);

}

}

 

 

 


Don't want to miss updates? Please click the below button!

Follow MindStick