Reputation: 101
I have started learning Java and picked up some books and collected materials online too. I have programmed using C++. What I do not understand is that ,even the main method is packed inside a Class in Java. Why do we have everything packed inside some class in Java ? Why it does not have independent functions ?
Upvotes: 10
Views: 7090
Reputation: 486
Update! The java team has been working on a feature called "Unnamed Classes and Instance Main Methods" in JDK21 and it's in preview mode. This has been launched to allow instance main methods. Such methods are not static, need not be public, and need not have a String[] parameter
So basically what if allows you to do is instead of writing the verbose syntax :
class HelloWorld {
void main() {
System.out.println("Hello, World!");
}
}
To make the class declaration implicit you can simply put:
void main() {
System.out.println("Hello, World!");
}
For more details you can refer to the official JEP here.
Upvotes: 0
Reputation: 114837
This is the main concept of object oriented programming languages: everything is an object which is an instance of a class.
So because there's nothing but classes in Java (except the few Java primitive types, like int, float, ...) we have to define the main
method, the starting point for a java application, inside a class.
The main
method is a normal static method that behaves just like any other static method. Only that the virtual machine uses this one method (only) to start the main thread of the application.
Basically, it works like this:
java MyClass
public static void main(String[])
That's it (in brief).
Upvotes: 8
Reputation: 5524
If you work in a smart IDE such as eclipse, you'll eventually find that Java's seemingly over-abundant restrictions, exceptions, and rigorous structure are actually a blessing in disguise. The compiler can understand and work through your code much better when the language isn't cluttered with syntactic junk. It will give you information about unused variables and methods, dead code, etc. and the IDE will give you suggested fixes and code completion (and of course auto-format). I never actually type out import statements anymore, I just mouse over the code and select the class I want. With rigorous types, generic types, casting restrictions etc. the compiler can catch a lot of code which might otherwise result in all kinds of crazy undetectable behavior at runtime. Java is the strictest language in the sense that most of what you type will not compile or else very quickly throw an Exception of one kind or another. So, if you ask a question about the structure of Java, Java programmers will generally just answer "because that's the rule" while Python programmers are trying to get their indentation right (no auto-format to help), Ruby programmers are writing unit tests to make sure all their arguments are of the correct type and C programmers are trying to figure out where the segfault is occuring. As I understand C++ has everything Java has, but too many other capabilities (including casting anything to anything and oh-so-dangerous pointer arithmetic).
Upvotes: 1
Reputation: 1475
And you can have multiple entry points in a jar depending on how many classes inside the package has main.
Upvotes: 0
Reputation: 11549
Java enforces the Object Oriented paradigm very very heavily. That said, there are plenty of ways to work around it if you find it cumbersome.
For one, the class that contains main can easily have lots of other methods in it. Also, it's not uncommon to make a class called 'Utility' or something similar to store all your generic methods that aren't associated with any particular class or objects.
Upvotes: 1
Reputation: 61643
Because that's how the designers of the language wanted it to be.
Upvotes: 1