I'm learning Java and I noticed that the main()
is put inside of a class. Why? I don't consider my main()
to be a member of any object. So please tell me how I can get around this.

- 71,713
- 13
- 134
- 174
-
1This is pretty much a duplicate of "[Why the main program in Java is put into a class?"](http://stackoverflow.com/questions/2155381/why-the-main-program-in-java-is-put-into-a-class?rq=1) – John Jan 23 '15 at 18:52
3 Answers
I don't consider my
main()
to be a member of any object.
It's not since it's a static
method. It does not belong to any object but to the class itself.
Aside from that, all methods, including main
must be defined in classes.
More generally, a class is the smallest unit in compiled Java code and contains both information on instances of the class and behavioral code that runs by itself (e.g. the main
method).

- 71,713
- 13
- 134
- 174
-
2The key here is that classes are not just blueprints for objects. Classes can do things by themselves, and `static` classes do everything themselves. A class is really just a self-contained chunk of code. – kgh Jan 23 '15 at 18:32
-
1Well, you can also put `main` and other methods inside of `enums`, too. There's also the `default` methods in Java 8 (but that doesn't cover `main`), so you don't *have* to have your methods defined in classes. – Makoto Jan 23 '15 at 18:34
-
1The main point is that a class is the smallest unit in the compiled code. All information are stored in classes, including the entry point for the application. – M A Jan 23 '15 at 18:35
-
2You should think of classes as solutions to concerns and application responsibilities. One responsibility of your executable is to run the application. That should be the one responsibility that your class with the main should address. – Isaiah van der Elst Jan 23 '15 at 18:39
By nature, Java is highly object oriented. So everything must be encapsulated within a class. All methods must be placed inside of a class. However, the main() is different. There can only be one main function in a class and it must always be static, meaning it is not part of an object and there is only one instance of it. When a java application is executed, the JRE will look for the main class (i.e. the class containing the main function). main() is where the execution starts. But due to the very nature of OO, it must be placed in a class. You can say that this is simply because of the skeletal structure of java. No other reason in particular.

- 41
- 8
You must put the main()
in a class. And, it must be static
(which means it is not a member of any Object
). When you launch the Java Runtime Environment (JRE) it will load that class and invoke the main()
.
This is covered by JLS-12.1 - Java Virtual Machine Startup which says in part,
The Java Virtual Machine starts execution by invoking the method
main
of some specified class, passing it a single argument, which is an array of strings. In the examples in this specification, this first class is typically calledTest
.

- 198,278
- 20
- 158
- 249