I started to write my first Java program.
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
The program runs fine just with the above code. But according to my OOP knowledge, a class is only an abstract concept and doesn’t come to life untill you create an object of its kind. And then, through that object we call the methods/functions inside the class.
But in this particular example, it seems that main method is called even without creating an object of the class HelloWorldApp
Is the object created somewhere else? If so, how does that portion of the code know my class name HelloWorldApp?
It is because it is
staticmethod, and for that it doesn’t need to create instanceJVM will load the
HelloWorldAppclass and it will invoke static method on it, And since it is public JVM (being external ) can access this methodAlso See