Thursday 23 May 2013

Java Main Method Interview Questions


1.Can we make main synchronized in Java?
Yes, main can be synchronized in Java,  synchronized modifier is allowed in main signature and you can make your main method synchronized in Java.

2.Can we overload main in Java ?

Yes you can overload main method in Java, nothing wrong with this but Java will only call your specific main method, i.e. main method with following signature :
public static void main(String[] args) or  public static void main(String args...) which is main method as variable argument method and only 


3.Can we override main in Java ?

No you can not override main method in Java, Why? because main is static method and in Java static method is bonded during compile time and you can not override static method in Java. If you declare method with same name and signature its called method hiding.

4.What if the main method is declared as private?
The code compiles properly but running the code as Java application will send en error message (stderr): “Main method not public”. view demo»

5.What if the static modifier is removed from the signature of the main method?
The code compiles, throws a java.lang.NoSuchMethodError at runtime. view demo»

6.What if I do not provide the String array as the argument to the method?
The String array is empty (not null). In C/C++ the first element is the name of the executable file. view demo»

7.If I do not provide any arguments on the command line, then the String array of main method will be empty or null?
See above, the String array is empty.

8.Can an application have multiple classes having main method?
Yes. When starting the application we specify the class name that needs to have a public static method called main that receives a String array parameter.

9.Can I have multiple main methods in the same class?
Yes, you can have multiple methods called main but only one with the signature main(String[]). Hint: it’s just method overloading. view demo»

10.What is the first argument of the String array in main method?
Unlike C/C++, the first element is NOT the name of the executable file. The first element is the first argument passed to the program.


More Questions on Main Method


EmoticonEmoticon