shimna
shimna

Reputation: 101

Why all the java code is packed in Classes?

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

Answers (6)

kalx11
kalx11

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

Andreas Dolk
Andreas Dolk

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:

  1. You start the application with java MyClass
  2. The JVM loads this class ("classloading")
  3. The JVM starts a new thread (the main thread)
  4. The JVM invokes the method with the signature public static void main(String[])

That's it (in brief).

Upvotes: 8

dspyz
dspyz

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

lalit
lalit

Reputation: 1475

And you can have multiple entry points in a jar depending on how many classes inside the package has main.

Upvotes: 0

DGH
DGH

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

Karl Knechtel
Karl Knechtel

Reputation: 61643

Because that's how the designers of the language wanted it to be.

Upvotes: 1

Related Questions