Java – Does the main method belong to any class


Does the main method belong to any class?

Best Solution

It does belong to a class. Look at any hello-world implementation and it would be clear to you.

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

won't compile as it stands. You need to put class HelloWorld { ... } around it, in which case you may say that the main method "belongs" to the HelloWorld class.

However, since it's static, it does not belong to any particular object. There is an important difference between classes and objects that you need to get acquainted with when working with object oriented languages. Learning Java: Objects and Classes is a good starting point.