In this article, we will list out reasons why do we use always “public static void main()” in Java
Why do we use “public static void main” only in Java ?
To start executing a Java program, the corresponding method should be “public static void main()”
- main : This is name of the method, which is used as a identifier by JVM to invoke or begin/start the execution of the program
- static : instance methods are invoked only after instantiation; but static methods can invoked with class name like <Class-Name>.<static-Method-Name>
- void : as the invoker of this method is JVM and it doesn’t need or expect any return value therefore it is void (as it indicate nothing to return)
- public : This is access-modifier or visibility & highest visibility is public, which is accessible for everyone
Main.java
package in.bench.resources.sample;
public class Main {
public static void main(String[] args) {
System.out.println("Hello, welcome to Java world !!");
}
}
Output:
Hello, welcome to Java world !!
Happy Coding !!
Happy Learning !!