2

As we know that the main method is the entry point of the java program as described in the following resources:

Why main() method is needed in java main class?

Why should main be present in a Java class? [duplicate]

If this is the case then why we need to wrap the main method in the class if the main method is the entry point?

enter image description here

What is the role of the class which wraps the main method?

Stack Overflow
  • 1
  • 5
  • 23
  • 51

4 Answers4

4

In Java, there's no way to have a method without an enclosing type. You need a class, an interface, an enum, etc. to be able to declare and/or implement a method. This is just how it works.

Even when you start a Java program, you specify the name of the class containing the main method. You don't just run arbitrary statements.

In other words it's about the program structure rather than about a specific role played by the class around the main method. And you can even use an interface instead of a class in recent versions of Java.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • `Even when you start a Java program, you specify the name of the class containing the main method. You don't just run arbitrary statements`, so, that is the reason why we use the same name of the `public class` for the` same java file` or `same file name` for the `public class` having the main method? – Stack Overflow Dec 20 '18 at 08:31
  • 1
    No. You can have a class named differently than its containing `.java` file (as long as it's not public). Java will create a separate `.class` file for it, so it will know where to find the class either way. – ernest_k Dec 20 '18 at 08:33
  • 1
    You don’t need to specify a main class. E.g. you can run a `jar` file with the `-jar` option or since Java 9, you can run a module, and since Java 11, you can even run a source code file. Of course, there’s still a class holding a `main` method behind the scenes. – Holger Dec 20 '18 at 08:34
  • Ohk, you mean there is still a `main` method internally assigned internally when the `class` file that is executed don't have any `main` mthod? – Stack Overflow Dec 20 '18 at 08:36
  • @Holger I believe those are just various ways of having/specifying the main class . Be it a manifest file, single-class program, etc., I guess the point is that there's going to be a class declaring the main method. – ernest_k Dec 20 '18 at 08:37
  • @SunnyKhan no, it means, you can create a module consisting of multiple classes and a particular class containing the `main` method being specified withing the module declaration. When you tell Java to run the module, it will automatically find out, where to look for the main method. – Holger Dec 20 '18 at 08:38
  • 1
    @ernest_k yes, that’s what I meant with “there’s still a class holding a `main` method behind the scenes”. It’s just that the analogy doesn’t work anymore, as only the program author needs to deal with the main class whereas the user who launches the program does not need to know it. – Holger Dec 20 '18 at 08:39
  • @Holger 100%. I was commenting more from the developer's perspective. Thanks – ernest_k Dec 20 '18 at 08:40
2

All code sits in a .class file, while an exception could be made for this method, doing so would add complexity to make such an exception, without much improvement.

As you need to put the code in a .class file and every file has in it a public class, interface, enum or interface with the same name as the file, you still need some way of declaring the name of the file in code. e.g.

Instead of

class Main {
    public static void main(String... args) { }
}

you can have

enum Main { ;
    public static void main(String... args) { }
}

or

interface Main {
    static void main(String... args) { }
}

but you will need to wrap main in something named which matches the file name.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

For a Java VM, class files are the only way to deliver executable Java code and Java libraries are just variations of containers containing class files.

When Java was created, there was a direct mapping of source level Java classes and class files, so the only way to generate such a class file containing executable code, was to declare a class.

Nowadays, the relation is not so strict, there are non-class artifacts of the Java language which could lead to the generation of a class file, e.g. package annotations or module declarations, further, classes may get generated by the runtime, like for lambda expressions.

So, in principle, there could be a new language construct to allow to define a stand-alone main method without a class declaration, which gets compiled to a class file behind the scenes. But since a typical application has only one entry point (and without a named hosting class, the possibility to define more than one would be gone anyway), it would not justify creating a new language construct.

Holger
  • 285,553
  • 42
  • 434
  • 765
  • So that is the reason why we use the same name of the `public class` for the` same java file` or `same file name` for the `public class` having the main method? – Stack Overflow Dec 20 '18 at 08:32
  • The Java language requires for `public` top level classes that the name of the `java` file matches the class name. But you can also run non-`public` classes (the `main` method must be `public` though) or nested classes. But using `public` top level classes is a good convention. – Holger Dec 20 '18 at 08:35
2

Because in contrast to other languages, there are no functions in Java.

Java only knows methods.

The difference: a method is tied to a class, functions are not.

Therefore, in order to have a first method to start with, that thing is still a method and needs an enclosing class.

In other words: in java, the only way to give a name to some piece of code is by turning it into a method. ( and you really want your "starting point" to have a name ) And methods need to go into a class, which is kind of implicit when looking at the Java language specification (chapter 8.2, class members).

GhostCat
  • 137,827
  • 25
  • 176
  • 248