Reputation: 2968
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?
What is the role of the class
which wraps the main
method?
Upvotes: 2
Views: 524
Reputation: 533492
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.
Upvotes: 2
Reputation: 140427
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).
Upvotes: 2
Reputation: 298143
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.
Upvotes: 2
Reputation: 45309
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.
Upvotes: 4