New changes to age old main method

In this post under Java, Language, I will introduce you to changes done to our age old main method as part of Java 25.

Pre Java 25, if we have to write our main method, we used to

  1. mark it public
  2. make the return value of the method “void”
  3. mark it static
  4. provide String array as method argument.

If any of these were missing we used to get compiler error or it was considered as ordinary java method with name same as Java main method.

Below is an example of old Java main method

package core.language;
public class NonInstanceMainMethodExample1 {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

Now as part of Java 25, the below actions are made optional

  1. mark it public
  2. provide String array as method argument

Now we can change the above class as shown below

package core.language;
public class NonInstanceMainMethodExample1 {
static void main() {
System.out.println("Hello World");
}
}

We can now execute the class without any issues.

Leave a comment