Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Java by (6.5k points)
Can anyone tell me why is the main method static?

1 Answer

0 votes
by (11.3k 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

0 votes
1 answer
0 votes
1 answer
asked Jan 27, 2020 in Java by angadmishra (6.5k points)

Browse Categories

...