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 ?
In Java, everything extends into an Object class. It means the coding is mostly wrapped in Java objects. The Java language assumes that you want to do only object-oriented programming. You cannot code anything in Java without declaring classes and objects.
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.
Every Java program requires the presence of at least one class.
classes - A class is a blueprint for building objects in Java. Every Java program has at least one class.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With