DEV Community

Cover image for Why is 'main' function static?
houdabelhad1
houdabelhad1

Posted on

Why is 'main' function static?

The main() method is defined as static because static methods belong to the class itself and not to an instance (object) of the class. This means you can call the method directly through the class without needing to create an object of that class.
If the main() method were not static, the JVM would be forced to create an instance of the class to call it. However, to create an instance, some code would already need to be executed, which would create an infinite loop (the instance would require the main() method to be created). This would make it impossible for the program to run.
The fact that the main() method is static ensures that there is only one entry point to start the program.
-Houda Belhad-

Top comments (0)