Search This Blog

Java main() Method Interview Questions with Answers

In this article, we will discuss Java main() method interview questions with answers.

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). You can only change the name of String array argument, for example, you can change args to myStringArgs.

Java main() method interview Questions with Answers

We will discuss below 8 Java main() interview questions and answers:

Why the main() method is public static?

Can we overload main() method in Java?

Can we declare main() method as private or protected or with no access modifier?

Can we declare main() method as a non-static?

Can we change the return type of main() method?

Can main() method take an argument other than String array?

Can we run define Java Class without main() method?

Can we make the main final in Java?

1. Why the main() method is public static?

If main() method is declared non-static then JVM (Java Virtual Machine) cannot call it without creating an instance of the class which contains the main() method.

While creating instance ambiguity might arise when the constructor takes an argument as which one to call.

2. Can we overload main() method in Java?

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 main() method with signature as public static void main(String[] args).

The below diagram demonstrates that the main() method can be overloaded:

String[] args main method called
Double[] args main method called
[1.0, 2.0, 3.0]

3. Can we declare main() method as private or protected or with no access modifier?

No, main() method must be public. You can’t define main() method as private or protected or with no access modifier. This is because to make the main() method accessible to JVM. Below diagram shows runtime error, if you define main() method other than public.

4. Can we declare main() method as a non-static?

No, main() method must be declared as static so that JVM can call main() method without instantiating its class. If you remove ‘static’ from main() method signature, the compilation will be successful but the program fails at runtime. Below diagram demonstrates that the main() method should be static otherwise JVM will throw runtime error:

5. Can we change the return type of main() method?

No, the return type of main() method must be void only. Any other type is not acceptable. Below diagram demonstrates that the main() method should have void return type:

6. Can main() method take an argument other than String array?

No, an argument of main() method must be String array. But, from the introduction of var args you can pass var args of string type as an argument to main() method. Again, var args are nothing but the arrays. Below diagram demonstrates that main() method should have an argument as String array or var args:

7. Can we run define Java Class without main() method?

No, We cannot define a class without main() method starting from Java 7. In the previous release of Java, we can have Static Initalizers as an alternative: