Author: SAI K
Core Java Interview
In this article, we will discuss frequently asked Java main()
method interview questions with
answers for both freshers and experienced.
As we know that Java main()
method is the entry point of any Java program. Its syntax is always
public static void main(String[] args).
We will discuss below 8 Java main()
interview questions and answers:
main()
method is public static?main()
method in Java?main()
method as private or protected or with no access modifier?main()
method as a non-static?main()
method?main()
method take an argument other than String array?main()
method?main()
final in Java?First, let's see why the main() method is static in Java?
The main() method is static in Java, so the JVM can directly invoke it without instantiating the class’s
object.
If the main() method is non-static, then JVM needs to create an instance of the class, and there would be
ambiguity if the constructor of that class takes an argument – which constructor should be called by JVM and
what parameters should be passed? We know that JVM can’t instantiate a Java class without calling a
constructor method.
The below example demonstrates why the main() method is static in Java?
package net.javaguides.corejava;
public class MainMethodDemo {
public MainMethodDemo(int arg0) {
//One argument constructor
}
public MainMethodDemo(int arg0, int arg1) {
//Two arguments constructor
}
public MainMethodDemo(String arg[]) {
}
public void main(String...args) {
//Non Static main method
}
}
Now, let's see why the main() method is public?
We know that anyone can access/invoke a method having public access specifier. The main() method is public
in Java because it has to be invoked by the JVM. So, if main() is not public in Java, the JVM won’t call it.
Yes, We can overload the main() method. A Java class can have any number of main() methods. But
to run the java class, the class should have a main() method with signature as public
static void main(String[] args).
The below diagram demonstrates that the main() method can be overloaded:
package net.javaguides.corejava;
import java.util.Arrays;
public class MainMethodDemo {
/** Actual main method with String[] args**/
public static void main(String[] args) {
System.out.println("String[] args main method called");
main(new Double[] {
1.0,
2.0,
3.0
});
}
/** Overloaded main method with Double[] args**/
public static void main(Double[] args) {
System.out.println("Double[] args main method called");
System.out.println(Arrays.toString(args));
}
}
String[] args main method called
Double[] args main method called
[1.0, 2.0, 3.0]
No, the main() method must be public. You can’t define the main() method as private or protected or with no
access modifier. This is because to make the main() method accessible to JVM.
The below diagram shows runtime error, if you define the main() method other than public.
No, the main() method must be declared as static so that JVM can call the main() method without instantiating
its class. If you remove ‘static’ from the main() method signature, the compilation will be successful but
the program fails at runtime.
The below diagram demonstrates that the main() method should be static otherwise JVM will throw runtime
error:
No, the return type of the main() method must be void only. Any other type is not acceptable.
The below diagram demonstrates that the main() method should have a void return type:
No, an argument of the main() method must be a String array. But, from the introduction of var args, you can
pass var args of string type as an argument to the main() method. Again, var args are nothing but the
arrays.
The below diagram demonstrates that the main() method should have an argument as String array or var args:
No, We cannot define a class without the main() method starting from Java 7. In the previous release of Java, we can have Static Initializers as an alternative:
public class MainMethodDemo {
static {
System.out.println("Static Initializer");
System.exit(0);
}
}
Error: Main method not found in class Test, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Static Initalizer
Yes, you can make the main() method final.
The below diagram demonstrates that we can have the main() method as final in Java.