Java 21: Simplifying Java for Beginners — Unnamed Classes and Instance Main Methods
Old Java:
Hey there Java beginners, let me introduce you to a feature that is going to save your lives. In the dark ages, beginners learning Java were required to learn a lot of concepts that are designed for large programs. For example, it is a sacred practice for software developers to write a simple hello world program. Let’s see how this hello world can be written in older versions of Java:
//Old Java (this was used in the dark ages to torture beginners)
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Somebody help me!");
}
}
The code given above was used to print a call for help “Somebody help me!” in Java. Now, this code has so many technical terms and concepts like ‘public’, ‘static’, and ‘String[] args’. This is more than enough to confuse a beginner or a developer shifting from a language like Python which does not require a lot of boilerplate code. The ‘main’ method is the starting point of every program in Java. JVM starts executing the program from here. You can say that this boilerplate code is a set of rules that are required to be followed in order for the JVM to be able to run the program.
Java 21:
Java 21 has made the lives of beginners easier by introducing instance main methods and unnamed classes. Let’s first remove some code:
public class HelloWorld {
void main() {
System.out.println("Hmmmmm");
}
}
This is what I mean by the instance main method. Now you do not need to add public, static, and String[] parameters. This version is somewhat easier to understand for those who have learned any other programming language. There is a return type ‘void’ which just means the method returns nothing and a System.out.println() statement which just throws anything you write in these double quotation marks “” on your console screen. Let’s remove this public class HelloWorld:
void main() {
System.out.println("Ez pz");
}
This is what I mean by unnamed classes. You can skip writing the name of the class. This is how beginners can now display something on their screen by writing a simple method with no need to create named classes and crazy keywords like static and public.
Java has changed the launch protocol for its programs making it flexible for programmers to define the entry point of the programs. In previous versions, JVM finds the static method with the String[] parameter to start execution. Now JVM will create a new Object itself if a non-private main method without a static keyword is found in the code.
What about static, public keywords?
Well, this feature is introduced to make Java beginner friendly. You will learn these keywords and concepts later. The only thing that changed is that you will learn these concepts gradually. It will help students write basic programs in a clear and efficient way, and allow them to gradually expand their code as they improve their skills.