Евгений Evgeniy Евгений Evgeniy - 11 months ago 72
Java Question

Java main method

Does Java Virtual Machine create any object of class, which contain main method? I think that answer is negative, because main method is static, and Java Virtual Machine needn't do it.

Answer Source

Yes, that's correct. The main method is static and doesn't need a receiver object, so the JVM will not instantiate an object of that type automatically. That's why you commonly see this idiom:

 public class MainClass {
     public static void main(String[] args) {
         new MainClass().doSomething();

Here, the main method explicitly creates a new object of type MainClass and then invokes some method on it to start everything up.