0 votes
1 view
in Java by (4.9k points)
Can anyone tell me why is the main method static?

1 Answer

0 votes
by (11.1k points)

The simple answer is" "Static functions don't require objects to run while non-static functions do."

The main function is the driver code of any program. If the main function is absent, we can only compile the code but can't execute it. The Java virtual machine needs to know what it has to execute and it cannot start executing a random program module unless it is commanded to. The main functions do exactly that and the execution of any program in Java does not occur without the driver code. If we make the main function non-static, JVM would have to create an object of the class the main function resides in to run the driver code as it stands and this will cause unnecessary and extra memory allocation.

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !