Does the main method belong to any class?
13 Answers
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.

- 413,195
- 112
- 811
- 826
-
4+1, Good catch - looks like the problem was the understanding of 'class' and 'object'! – Andreas Dolk May 27 '10 at 11:34
Yes. Every method or field must belong to a class (or interface/enum).

- 302,674
- 57
- 556
- 614
Every line of Java code (except import/package) lives in a class (or is a class/interface declaration). So does main
.

- 181,842
- 47
- 306
- 310
If you mean whether it belongs to every class, that is not the case. It belongs to the class where you define it.
But any class can have a static main
method.

- 795,719
- 175
- 1,089
- 1,143
The main method in Java belongs to a class but not to an Object. Objects are created at run time. Thus because the main main()
in Java is the start point in your application, there is no way to start your application from an instance specific method. That is why the static
keyword make perfect sense with the main method. In fact all the parts of the main method declaration make perfect sense when you think like the 'jvm' and picture what the main method does (starts the application):
public
, because this method must be accessible by the jvm (not written by you).static
, implying this method can be accessed without having an object (because it's representation never changes), but here the logic is easy understood if you think like the jvm again; "I don't have any objects to create (instantiate) objects, so I need a static method to start the application as there simply isn't any logical way to get an instance specific method up yet as I don't have anything up yet to create objects".void
This method can't logically return anything because there is nothing up yet to return anything to. It is the start point of the application.main
I am the main method as without me you won't have an application.String[] args
Send me data you may feel useful for my startup.

- 9,339
- 6
- 34
- 51
In Java any application must have the main
method in any of the classes. And it needs to be exactly of the formula:
public static void main(String[] args)
See more in official lessons.

- 68,043
- 8
- 59
- 60
-
1If you count a web application as an application then that's not true. A web application does not need a `main` method. – Joachim Sauer May 27 '10 at 11:20
-
1But to execute your web app, a `main()` method must have been run... you just didn't have to write it. – Dolph May 27 '10 at 14:21
Any class in java can have a public static void main(String[] args). The main function that is declared within a class (like any other static method) belongs to the class definition but not it's instantiation instance.
If you are building a JAR file from a collection of classes, you can specify which class within the JAR contains the application main method in the META-INF/Manifest.mf using the
Main-Class: fully qualified name of class (example: ie.mycode.MyApp)
When you run the command
java -jar [your jar file]
It will look at the manifest and start executing code specified in the main for the Main-Class object.

- 2,240
- 13
- 11
As mentioned before, it is part from a class, but not any class
If your class in the the "unnammed" or "default" package, you main method won't be to call any other Java classes from that initial Class with its main method().
While it can work, it will limit considerably what you can actually do within that main
method.
So don't use any class (i;e; not one in the default package)
It must belong to a class, as with any method, and it must be in the class you wish to execute after compilation.
Programs can only begin by executing a class which has a main method (note: this is applicale for most types of java applications. Applets, for example, work differently)

- 1,432
- 1
- 10
- 8
its not like that main method belongs to any specific class it belongs to that class in which we define main method. it can be any class

- 6,991
- 8
- 35
- 67
main
is a normal method. The only convention is that Java can use this (and only this) method to 'start' an application. If you pass a class to java.exe, it tries to reflect this method (that's why the method signature is absolutly strict!) and invokes it (if found). This, in fact will 'start an application' (iaw: the first thread).

- 113,398
- 19
- 180
- 268
When we try to run a class whose path is not known to the JVM, then you get an error/exception something like below
Exception in thread main: classdefnotfound........
If main belongs to the class we are trying to run then why the error says exception in thread main
this can happen only when a thread main is running
We often use interfaces to define certain contracts. Like the Runnable interface which defines the run() method which will be called by executors. Alternatively this could be a class with an abstract method, conceptually it's the same.
You also need such a contract for the entry point of your Java application. It was quite natural that you were looking for it somewhere in base Java classes like Object.
However, the main() method is different. Its "contract" is defined not by an interface but by specification, i.e. Java Language Specification. The whole §12 is dedicated to execution and §12.1.4 specifies the "contract" for the main() method.
On a side note, it is not so rare that certain "contracts" are not defined by interfaces or abstract methods but via formal specification. Another example of this are readObject() and writeObject() methods.

- 1
- 3