tags:

views:

282

answers:

9

Why does the main method have to be put into a class? I understand the main ideas of OOP but I cannot get why the main program is defined within a class. Will such a class instantiated somewhere? I mean there is no code outside the class. What is a reason to define a class and never use objects of this class?

+15  A: 

The VM has to start the application somewhere. As Java does not have a concept of “things outside of a class” the method that is called by the VM has to be in a class. And because it is static, no instance of that class is created.

Bombe
A: 

The main reason is so that multiple classes can have a main method. So a codebase can have many "entry points" and one just uses the class to specify which one is called. Also, this is inline with the OO design where (almost) everything is an object.

Michael Easter
Can I have many entry points at the same time? For example I have the "main" class with the method called "main". What will happen if within this class I use another class which also has method called "main"?
Roman
Roman, maybe you are better off reading a basic OOP tutorial, you don’t seem to have the full grasp on it yet. :)
Bombe
A: 

Main in java is a static method, therefore the class it's in doesn't need to be instantiated into an object, the class simply needs to be loaded.

Tom
+5  A: 

It does simplify the design of the virtual machine. Since virtual machine already knows how to run a static method of a class then it can treat the main method just as any other static method.

If you put the main method in any other construct other than a class then the VM has to be modified to know about another construct complicating things even more.

Vincent Ramdhanie
A: 

That's simply how Java was designed: (almost) everything is an object, and code can only exist as part of a class.

Since the main() is static, it being called does not automatically lead to an instantiation of the class. However, it's perfectly possible (and quite common, at least in small Swing programs and Applets) to have the class that contains the main() be an otherwise normal class that is instantiated and used like any other class.

Michael Borgwardt
A: 

As we know main is the entry point for the JVM to start. And in java there is nothing except classes and interfaces. So we have to have a main method in the class that too it should be a public class. and the main should always be public static, because it should be accessible for JVM to start and static because it starts without creating any objects

GK
+2  A: 

When the Java language was designed, the notion that everything must be an object was a point of dogmatism. (though they left in a few primitive types). These days you could perhaps design a language that uses a closure -- even one outside any class -- instead.

Drew Wills
+2  A: 

What @Bombe said. I would add that to OO purists, the fact that the entry class is not instantiated is a misstep. The reason is the static main prevents someone from writing a family of main classes that share the same main() method written using a template method pattern.

If Java had been written to instantiate the main class and invoke the main method, users would have enjoyed the benefits of inheritance and interfaces.

Dilum Ranatunga
Coincidentally, I saw this <http://stackoverflow.com/questions/2154802/overriding-a-java-method> right after posting my answer. This user would have benefited from non-static main method.
Dilum Ranatunga
It would be nice if Java *required* an app class extending Application with the arguments as a constructor, similar to how applets work.
Chris Dennett
+6  A: 

While it's true that java has no concept of methods outside of classes, it would be pretty easy to allow developers to skip all the broilerplate of actually writing out the class definition and simply let people write the main function directly, if they wanted too.

But back when people were developing java, people were more dogmatic about OO then they are now, and that probably played a big part. Think of it as an example of Opinionated software

It has one useful feature though, which that you can main functions to any class and launch them as programs. It's handy for testing specific features of those classes when you're working on them. If main() had to be in a separate file, and not part of a class, that would make things difficult. What if you want more then one main() function? What if you wanted to write functions to be called by main outside of the class? Would they go in a global namespace? Would multiple main() functions collide?

Not like those problems are impossible to solve, but other then a little extra typing, which can be especially annoying for newbies the current solution isn't too bad, especially since IDEs will generate the basic class structure when you create a new file.

Chad Okere
+1 for mentioning the possibility to have multiple `main`, which is handy sometimes
ewernli