Why does the main function in Java reside in a class?

class, java

Solution

Probably for the same reason you put question marks at the end of a question: that's just how they decided it's done.

The `main` method is the result of a convention that says "this is how the entry point's method signature should look" which doesn't exempt it from language semantics.

Java does not support methods outside of classes/interfaces and as such it has to be contained in one.

Problem

I just started studying Java. And the main function of the program always resides in a class. ``` public class t{ public static void main(String[] args){ // do stuff } } ``` I have studied C++, and in there the main function doesn't need to be in a class. Why in Java we have to do that? Why can't the main function exist in Java without a class, like it does in C++?

Original source