photo of orange yellow and red hello molding clay

Java 21 Features – Unnamed classes and instance main methods

photo of orange yellow and red hello molding clay
Photo by Maria Tyutina on Pexels.com

One of the complaints against Java language is its verbosity.

Too much code to achieve too simple tasks.

But Java was originally developed to solve complex enterprise problems in mind.

So not much attention was paid to newbie developers who find it difficult to learn the language.

For example,

How do you print “Hello World!” in Java?

You create a class.

It should be public.

You then create a method.

The method should be named “main”.

The main method should be public (prefixed with public keyword)

The main method should be static (prefixed with static keyword)

The main method should have a String array argument.

The main method should return void.

To just print a message,

You should use System.out.println – three words.

Example:

public class HelloWorld{

        public static void main(String[] args){

              System.out.println("Hello World!");
       }
}

So many rules just to print a text!

No wonder a beginner can find it demotivating.

Why to pass arguments if I dont want to accept any user input?

Why should it be static?

What is static?

Why should it be prefixed with public keyword?

What is this public keyword for?

Why do we need a class just to print a text?

Questions like this can trouble a beginner.

To know these concepts it might take a while.

This slows down their learning curve.

Keeping this in mind , Java has now removed several of these rules.

You still need a main class to launch a Java program.

But,

You don’t need a class!

You don’t need to prefix the main method with public keyword!

You don’t need to prefix the main method with static keyword!

You don’t need to accept any arguments in the main method!

All you have to do is the below:


void main(){

          System.out.println("Hello World!");

}

And then save the above line of code in a file with any name ending with “.java”!

This is a paradigm shift for a Java developer who has learned that every code goes inside a class and the class name should match with the file name!

Let me test it out.

I downloaded the latest early access builds of Java 21 and copy pasted the above code in a file named HelloWorld.java

And then I ran the file using “java” command with the preview feature enabled (since JDK 21 is not yet released to the public as of this writing).

You also need to include ”source” flag while running java command if you are using “enable preview” flag.

As you see Hello World! got printed.

Good news for Java newbies!

Java is also planning to simplify System.out.println() statement in the future.

Now what about creating other methods in the same Java file?

Do we have to create a static method as Java developers used to do ?

No.

These can be plain methods as shown in the below example:

String greeting() { return "Hello, World!"; }

void main() {
    System.out.println(greeting());
}

You can save the above code in a file and run it.

That will print the same output too!


Posted

in

,

by

Comments

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading