Java main() method Interview Questions with Answers
Q1 Why the main() method is public static in Java?This is one of the most important questions in the main() method interview questions.
a. Why the main() method is static in Java
As we all know at the time of starting JVM, there is no object of a class. We add the static keyword to the main() method so that JVM can load the class into the main memory.
If we declare the main() method as non-static then JVM can not call it without creating the object of the class which contains the main() method.
If we try to create an object, it has to call the constructor of that class. There would be ambiguity if there is more than one constructor present in the class - which constructor should be called by JVM and what parameters should be passed?
b. Why main() method is public in Java
A method with a public modifier can be accessed or invoked by anyone. The reason behind making the main() method public is that it has to be invoked by the JVM. If you assign an access modifier other than public to the main() method then compilation will be successful but you will get the runtime error "main method not found".
Q2. Can we overload the main() method in Java?
Yes, we can overload the main() method in Java. In other words, a Java class can have any number of main() methods with different parameters. But execution will only start from the main() method with the following signatures:
public static void main(String[] args)
or,
public static void main(String args...)
Let's find out with the help of an example that the main() method can be overloaded in Java:
import java.util.*;
public class MainOverloadExample {
// Real main() method with String[] args
public static void main(String[] args) {
System.out.println("String[] args main method");
main(new Integer[]{10, 100, 1000});
}
// Overloaded main() method with Integer[] args
public static void main(Integer[] args) {
System.out.println("Integer[] args main method");
System.out.println("Printing Integer[] args: " + Arrays.toString(args));
}
}
Output:
String[] args main method
Integer[] args main method
Printing Integer[] args: [10, 100, 1000]
Q3. Can we override the main() method in Java?
No, overriding the main() method is not allowed. Why? because in Java static method is bonded during compile-time and you can not override the static method in Java. If you define a static method in the child class(subclass) with the same method signature as the parent class(superclass) then it is called method hiding in Java.
Q4. What are the valid access modifiers(public, protected, default, private) for the main() method in Java?
The public is the only valid access modifier for the main() method in Java. You will get a runtime error if you use a private, protected, or default access modifier with the main() method in Java as shown below in the example:
public class MainExample2 {
// Using protected access modifier for main() method
protected static void main(String[] args) {
System.out.println("Inside main method");
}
}
Output:
Error: Main method not found in class MainExample2, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Q5. Can we make the main() method final in Java?
Yes, you can make the main() method final in Java. Like any final method, the main() method can not be overridden since it is static. Please find the example below:
public class MainExample3 {
// Using final keyword with main() method
public static final void main(String[] args) {
System.out.println("we can make main() method final");
}
}
Output:
we can make main() method final
Q6. Can we make the main() method synchronized in Java?
Yes, we can make main() method synchronized in Java as a synchronized modifier is allowed in the main() function method signature as shown below in the example:
public class MainExample4 {
// Using synchronized keyword with main() method
public static synchronized void main(String[] args) {
System.out.println("Valid to use synchronized keyword with main() method");
}
}
Output:
Valid to use synchronized keyword with main() method
Q7. Can we change the return type of the main() method in Java?
No, we can not change the return type of the main() method in Java. It should be void. We will get a runtime error when we change the return type of the main() method in Java as shown below:
public class MainExample5 {
// Change the return type of main() method
public static int main(String[] args) {
System.out.println("Changed the return type of main() method");
return 0;
}
}
Output:
Error: Main method must return a value of type void in class MainExample5, please
define the main method as:
public static void main(String[] args)
Q8. How to call a non-static method from the main() method in Java?
Always remember, non-static methods can not be called directly from the static context. First, you need to create the object of the class containing both static and non-static methods inside the static method. The next step is to call a non-static method using that object as shown below in the example:
public class CallingTest {
// main() static method
public static void main(String[] args) {
// Creating CallingTest class object
CallingTest obj = new CallingTest();
/* Calling non-static print() method from
static main() method */
obj.print();
}
// print() non-static method
public void print() {
System.out.println("Non-static method is called inside static main method");
}
}
Output:
Non-static method is called inside static main method
That's all for today, please mention in the comments in case you have any questions related to Java main method interview questions with answers.