VOOZH about

URL: https://www.geeksforgeeks.org/dsa/what-will-happen-if-i-declare-main-in-java-as-non-static/

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


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 27 Dec, 2023

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?

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:

  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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:


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.

Comment
Article Tags: