Starting point for a java application (not always ) is this method
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
When you do java className
it will go and check if the class has a main method, since its static it can be called without creating an instance.
If there is no main method or main method is there but not with the same signature it will throw you a RuntimeException
stating main method not found.
Don't forget to read A closer look at main method.
off topic:
Extending the same idea, you don't need an instance of a class to refer its static method and fields.
public class MyClass {
public static int NUMBER = 10;
public static void String sayHello(){
return "Hello";
}
public void String sayBye(){
return "Bye";
}
public static void main(String[] args){
System.out.println(NUMBER); // No need for object
System.out.println(sayHello()); // No need for object
System.out.println(new MyClass().sayBye()); // sayBye is not accessible at class level have to have an object of MyClass to access sayBye
}
}
If same is called in some other class then it may look like:
public class MyClassCaller {
public static void main(String[] args){
System.out.println(MyClass.NUMBER); // No need for object just refer the class
System.out.println(MyClass.sayHello()); // No need for object just refer the class
System.out.println(new MyClass().sayBye()); // sayBye is not accessible at class level have to have an object of MyClass to access sayBye
}
}
A nice discussion on usage/overusage of static methods