Open In App

What will happen if i declare main() in java as non-static?

Last Updated : 27 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

main() method in Java acts as an application’s entry point. So declaration must read “public static void main(String[] args)” to work properly .

What happens if you declare main to be non-static is as follows?

Java




import java.util.Scanner;
public class Sample{
   public void main(String[] args){
      System.out.println("This is a sample code");
   }
}


Compilation Error:

If we declare main as non-static (public void main(String[] args)), our code will not compile. This is because the Java Virtual Machine (JVM) needs to call the main method without creating an instance of the class. If main is non-static, it would require an instance of the class to be created, which contradicts the purpose of the entry point method.

Reasons Why main() Method Should be Static:

  • No Need to Create an Instance of the Class Containing the Main Method: Making the main method static removes the need to create an instance of the class. Java applications don’t need to create a class object in order to begin running from the main function. This is essential because it simplifies the program startup process because the Java runtime environment allows you to invoke a static method directly without generating an object.
  • Entry Point: The main method is the program’s entrance point, and it is used by Java applications. Being static indicates that it belongs to the class as a whole, not to any particular instance. In order to begin executing a Java program, the JVM searches for the public static void main(String[] args) function. Main must be static since non-static methods cannot be invoked straight from the JVM without an instance.
  • Memory Efficiency: Static methods are kept in a different location of memory because they belong to the class rather than any specific object. Memory efficiency is improved by separating static procedures from object-specific data (instance variables). In contrast, static methods do not need to allocate memory for each instance of an object, but non-static methods do.
  • Consistency Across Java Applications: By mandating that the main method be static, Java applications are guaranteed to be consistent. The signature of the main method is constant regardless of the class or application. This uniformity makes it easy for developers to comprehend how Java applications begin their execution and streamlines the process of starting Java programs.
  • Activating Command-Line Invocation: Java programs are frequently launched from the command line with a class name specified. If the main method wasn’t static, the command to run the program would have to specify extra information about how to make a class instance, which would make the command-line execution process more difficult. The main method can be called directly because it is static, which makes command-line calling easier.
  • Static methods are simple and thread-safe: Because they don’t need instance-specific data when they operate so these are simple and thread-safe. Making the main method static promotes thread safety because it is frequently where multithreaded Java applications begin. Additionally, a static main method makes it easier to comprehend how a program works because there is just one entry point, which improves predictability and manageability.

Example of using static:

Java




// Java Program to implement
// Direct Addition to Add two Numbers
import java.io.*;
 
// Driver Class
class GFG {
    public static int sum(int num1, int num2)
    {
        return num1+num2;
    }
     
    // main function
    public static void main(String[] args)
    {
        GFG ob = new GFG();
        int res = ob.sum(28, 49);
        System.out.println(res);
    }
}


Output

77




Conclusion:

Declaring the main method in Java as non-static is not possible. The Java compiler will generate an error message if you try to do so.

This is because the JVM expects the main method to be static. If main() is non-static, it would require an instance of the class to be created before execution, which contradicts the fundamental purpose of the main() method as the starting point of the program.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads