What will happen if I declare main in java non-static [duplicate]


I recently started learning Java and the first doubt I encountered is main is declared static in java so that JVM don't have to initialize the class for accessing main. But my question is why JVM avoid initializing the class which has main? What cost does it incur if we declare main as non-static?


Answers:


The compiler will treat as an instance method, i.e. you won't be able to execute it directly with java YourClass.

But my question is why JVM avoid initializing the class which has main?

Because there's no need of initializing the class, since it cannot be an entry point to the application. At Runtime, JVM will check for the presence of a public static void main(String[] args) method and if there is one, then it will initialize the class (i.e. execute all it's static blocks, initialize it's static variables and so on). However, if the class is considered as in invalid for being an entry point to the application, the operation will break.