Skip to content
Ramesh Fadatare edited this page Oct 12, 2018 · 3 revisions

In this article, we will discuss an important 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:

  1. Why the main() method is public static?
  2. Can we overload main() method in Java?
  3. Can we declare main() method as private or protected or with no access modifier?
  4. Can we declare main() method as a non-static?
  5. Can we change the return type of main() method?
  6. Can main() method take an argument other than String array?
  7. Can we run define Java Class without main() method?
  8. 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.

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
    }
}

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).

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));
    }
}

Output:

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

3. Can We Declare Mmin() 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.

diagram here

4. Can We Declare main() Method As Non-Static?

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

diagram here

5.Can We Change 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 main() method should have void return type:

diagram here

6. Can main() Method Take an Argument Other Than String Array?

No, 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 argument as String array or var args:

diagram here

7. Can We Run Define Java Class Without main() Method?

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

public class MainMethodDemo
{
    static
    {
        System.out.println("Static Initalizer");
        System.exit(0);
    }
}

Output: (From JDK 7)

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

Output: (JDK 6)

Static Initalizer

8. Can we make main final in Java ?

Yes, you can make the main() method final. Below diagram demonstrates that we can have main() method as final in Java.

diagram here

Clone this wiki locally