Related Articles

Related Articles

Does JVM create object of Main class (the class with main())?
  • Difficulty Level : Easy
  • Last Updated : 07 Jan, 2020

Consider following program.

filter_none

edit
close

play_arrow

link
brightness_4
code

class Main {
    public static void main(String args[])
    {
        System.out.println("Hello");
    }
}

chevron_right


Output:

Hello

Does JVM create an object of class Main?
The answer is “No”. We have studied that the reason for main() static in Java is to make sure that the main() can be called without any instance. To justify the same, we can see that the following program compiles and runs fine.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Note Main is abstract
abstract class Main {
    public static void main(String args[])
    {
        System.out.println("Hello");
    }
}

chevron_right


Output:

Hello

Since we can’t create object of abstract classes in Java, it is guaranteed that object of class with main() is not created by JVM.



This article is contributed by Narendra Koli. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :