Java main Evolution (or not?)

The JEP 445 introduces a preview change to the way Java recognizes a main method. While I don’t believe this a great change, nor a needed one, apparently the main method is something new developers cannot deal with. And I have proofs about this, since no student seems to be able to write a main without a signature error. Despite the above, and my personal considerations, Java is going to support some changes to the main method, that so far has to be:
  • static, not an instance method;
  • public, visibile from anywhere;
  • void, cannot return any value;
  • accepting a String[] single parameter.
Changes made include:
  • unnamed classes that is a source unit (a file) could avoid the declaration of a class;
  • remove mandatory arguments so that main can skip the String[] argument;
  • allows for instance main method, so that the method does no more require public nor static.
Therefore, while you should write:

public class Main {
  public static void main( String a[] ) { ... }
}


you can now write the following:

void main() { ... }


Great job Java: you are catching up C! Let’s see how the code changes:

public class Main {  // removed by unnamed classes

  public static // removed by instance main
         void main( /* String args[] */ ) { ... }

} // removed by unnamed classes


I don’t like the idea at all. After all, it is not such a big deal to write a fine main method, so I don’t see why there is the need to refactor the launching protocol to handle this changes. In fact, in order for the JVM to understand which main method to run, the launching protocol has to be changed accordingly, and quite frankly I believe that newcomers and students will be confused by different main methods. As an example, consider the following:

public class Main {
   public static void main( String a[] ) { ... }
   void main() { ... }
}


In the above, only one main is executed, and that depends on the JVM launching protocol. But is it clearer to a newcomers what happens? I don’t think so…

The article Java main Evolution (or not?) has been posted by Luca Ferrari on September 28, 2023

Tags: java