What will happen if we don't declare the main as static?

If the main() method is not declared as static, the JVM cannot invoke it to start the application. This is because the JVM calls the main() method before creating any object of the class, and a non-static method requires an object instance to be invoked.

Key Points: • The JVM uses the main() method as the entry point of a Java application. • A non-static method can only be called through an object reference. • At application startup, no object exists, so the JVM cannot invoke a non-static main() method. • The program may compile successfully, but it will fail at runtime when the JVM attempts to locate the correct main() method. • Declaring main() as static allows the JVM to call it directly without creating an object.

Example: If the main() method is declared without the static keyword, the JVM cannot start the program because it has no object available to invoke the method.

Code Example:

public class Test {

    public void main(String[] args) {
        System.out.println("Hello");
    }
}

Runtime Error:

Error: Main method is not static in class Test, please define the main method as:

public static void main(String[] args)

Interview Tip: A concise interview answer is:

"If the main() method is not static, the JVM cannot invoke it because no object exists when the application starts. Therefore, the program will not run, and the JVM will report that the main method must be declared as static."