Top 5 Java Main method Interview Questions with Answers

Hello Java programmers, the main() method in Java is the starting point of any standalone core Java application. JVM starts executing Java program from main method and the thread which executes main is called main thread in Java. The main method is also an important topic in Java interviews for 2 to 3 years of experienced developers. In this Java article, we will a couple of questions related to the main method in Java. Apart from Why main is static in Java, I see the following questions keep coming related to the main method:
  1. Can we overload the main method in Java? Which main method JVM will call?
  2. Can we override the main method in Java?
  3. Can we make the main final in Java?
  4. Can we make the main synchronized in Java?
  5. How to call a non static method from main in Java?


5 Main Method Interview Questions with Answers

Now that we have seen some common Java interview questions related to the main method, let's find out their answer and understand some key Java concepts related to the main method. 

1. Can you overload the main in Java?

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


2. Can you override main in Java?

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

3. Can you make the main final in Java?

Of course, you can make the main method final in Java. JVM has no issue with that. Unlike any final method, you can not override main in Java.


4. Can you make the main synchronized in Java?

Yes, the main can be synchronized in Java, a synchronized modifier is allowed in the main signature and you can make your main method synchronized in Java.


5. How to call a non-static method from main in Java?

This question applies not only to main but all static methods in Java. Since nonstatic methods can not be called from static context directly, you need to first create an Object as a local variable and then you can call a non-static method using that object, as shown in the following example:

10 points about Main method in Java


Now, let's see a Java Program to see how to call a non-static method from a static method like the main method in Java. 


import
java.util.Date;


/**
 * Java program to show how to call non-static method from the static method in Java
 *
 * @author http://java67.blogspot.com
 */

public class StaticTest {

    public static void main(String args[]) {
     
        // calling non static method from main in Java
        //printCurrentTime(); //compile time error - can not call non static method from main
     
        StaticTest test = new StaticTest();
        test.printCurrentTime();
     
    }
 
 
    public void printCurrentTime(){
       System.out.println(new Date());
    }
}

Output:
Tue Nov 06 19:07:54 IST 2012


Summary

Here are some important points about the main method in Java which every Java Programmer should know and remember:
  • The main() method is a static method
  • You can overload the main() method in Java.
  • You cannot override a main() method in Java
  • You can make the main method final in Java
  • You can make the main method synchronized in Java.
  • You cannot call a non-static method from main in Java. 

That's all about the main method in Java. So these were some of the frequently asked questions about the main method in Java. This does not only help to answer interview questions but also to build your concept on the main method in Java.


Other Java Interview Questions you may like
Difference between ConcurrentHashMap and HashMap in Java

And, lastly one question for you, can you run a Java program without main() method? If yes, how? can you please explain?

3 comments:

  1. I never thought that someone will ask main() method related question on core Java interviews but I was wrong, they do, especially why main is public static and void in Java?

    ReplyDelete
  2. Public static void main(String[] args)
    Public to be called by JVM from anywhere
    Static so that JVM can call this method without existing object
    Void so that main method not return anything to JVM
    Main because is name which is configured in the JVM
    String [] args command line arguments

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.