ooodddbbb
ooodddbbb

Reputation: 121

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

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++?

Upvotes: 10

Views: 7289

Answers (7)

shubham
shubham

Reputation: 1

since java is oop language , u cant do anything unless u create a class. this makes java object oriented language. and java was designed like that to first to load the public class and call main method

Upvotes: 0

Harry
Harry

Reputation: 101

Let's consider that following is allowed in java

void main (){
  f1();
  f2();

}
int f1(){
  f2();
  return 1;
}
int f2(){
   return 3;
}

since there are no prototypes in java how would main call f1 or f2 or how would f1 call f2? i Think that this is one of the reasons

Upvotes: -1

bhspencer
bhspencer

Reputation: 13560

The "every method in Java must be in a class because the spec says so" answer is only part of the story. By having main() inside a class it is possible to have multiple entry points within a project. i.e. multiple classes with main() methods. This allows you to select a different main class at runtime rather than compile time. e.g.

java -cp program.jar com.example.Class1

or then if you want to run a different main from the same jar

java -cp program.jar com.example.Class2

Upvotes: 3

MinecraftShamrock
MinecraftShamrock

Reputation: 3574

Because everything is inside a class in Java. Java code consists only of classes. They may be nested in themselves or in a packagehierarchy, but nothing is allowed to appear outside of a class.

The only exceptions are the package and import statements.

Upvotes: 2

M A
M A

Reputation: 72844

  1. All methods must reside in a class in Java.
  2. main is a method.

Therefore, main must reside in a class.

Furthermore, a class is not just a blueprint of an object. It is by itself a chunk of code that does something. One of the things it does is providing the entry point of the application by containing a main method.

Upvotes: 1

TheLostMind
TheLostMind

Reputation: 36304

Because that is how the language was designed. The JVM first loads the class containing the main function and then calls the main() method. Without loading the class, main() cannot be called, i.e, main() doesn't exist without its enclosing class.

Upvotes: 0

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44439

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.

Upvotes: 6

Related Questions