Unnamed Classes in Java

Unnamed classes and learning Java: image of 2 despondent people at desks with some Java code above them.

The Challenge of Learning Java

I know that I promised to continue discussing class loaders in this post, but I’ve just read an updated JEP (JDK Enhancement Proposal) that I thought would be interesting to share. It’s about unnamed classes and the instance main method.

I believe Java is a great language to program with! It has a huge ecosystem of frameworks and libraries that help us deliver professional software easier and quicker than ever before.

But how often do we spare a thought for the novice programmers; those starting on the same journey of discovery that we travelled?

In many ways, Java can be pretty difficult to learn. We have to learn about classes and objects. We need to know about access modifiers and the static keyword. We have to learn about Strings, arrays and methods. And this is before we even know anything about variables, control statements and data types.

The Standard Hello World Program

Think about the very first HelloWorld program that most of us have written:

public class HelloWorld { 
    public static void main(String args[]) { 
        System.out.println("Hello, world!");
    }
}

There’s too much unnecessary infrastructural code here for a beginner to get to grips with at first.

Let’s look at it in more detail:

  • The class declaration is important when structuring a larger program. We use classes for encapsulating attributes and behaviours. But it’s really unnecessary for a simple program printing out “Hello, world!” .
  • The public access modifiers are important for creating well-defined interfaces, but too much to handle right now as a beginner. Having the public keyword then opens the door to needing to know about the other access modifiers: private, protected and default. And default is the worst to understand because we don’t even use the default keyword. That is used for other things. And default actually means package-private. This means somewhere along the line — sooner rather than later — we have to introduce the package concept to beginners.
  • And the String args[] parameter? It’s compulsory, otherwise the JVM won’t run the program, but most of the time we never use it. We only use it when we need to pass data from the operating system’s command line interface. And that’s scary enough for a lot of experienced programmers.
  • The static modifier is the worst to explain to a beginner. It’s part of Java’s class-and-object model. When we know all about classes and objects it makes sense, but if we haven’t got a clue about them, it’s totally confusing. Worst of all, if a beginner wants to add an extra method to call from main(), then they have to mark it static as well. This isn’t a good habit to get into. Or they need to learn about the differences between classes and objects, and how to instantiate objects. This is even before they’ve got to grips with variables!

We can tell beginners “Don’t worry about these things right now. They’ll make sense later.” This isn’t very satisfying to them and to us. It leaves beginners with the scary feeling that Java is difficult to learn. I know this, because we offer the Introduction to Java Programming course for beginners.

The Proposal for Unnamed Classes

The JDK Enhancement Proposal I read was JEP 445: Unnamed Classes and Instance Main Methods. This is a preview feature in JDK 21.

In it, they proposed an enhancement to make it easier for beginners to write their first Java programs. Beginners will be able to write single class programs without needing to understand the language features needed for large, multi-class programs.

When beginners first start to program they write small programs by themselves. They don’t need modules, packages, classes and encapsulation. They need to learn the basic concepts of variables, types, control statements and procedures.

The JEP introduces two features: instance main methods and unnamed classes.

An instance main method is essentially a simplified main() method. It is not static, doesn’t need to be public and doesn’t need a String[] parameter.

So we can write the classic “Hello, world!” program as follows:

class HelloWorld { 
    void main() { 
        System.out.println("Hello, world!");
    }
}

Unnamed classes allow us to leave out the class declaration. This is then implicitly assumed:

void main() {
    System.out.println("Hello, world!");
}

We can even add methods and fields to this unnamed class as follows:

String goodbye = "Good bye!";

String sayHello() {
    return "Hello, world!";
}

void main() {
    System.out.println( sayHello() );
    System.out.println( goodbye );
}

Conclusion

With these enhancements, learning Java will be a lot easier for beginners. Writing small programs as unnamed classes allows a beginner to focus what the program actually does. They won’t need to know about advanced concepts from the start.

Once a beginner grasps the class and object concepts, it’s very easy to change an unnamed class into an ordinary class. All they need to do is wrap the unnamed class inside an explicit class declaration. The import statements will obviously have to be moved out of the class to the correct place in the file.

There are a lot of technical details in the proposal which I’ve left out here. For further information, see the complete JEP 445 proposal.

Was this post useful? Please share your comments, and as always, stay safe and keep learning!

Leave a Comment

Your email address will not be published. Required fields are marked *

Thank You

We're Excited!

Thank you for completing the form. We're excited that you have chosen to contact us about training. We will process the information as soon as we can, and we will do our best to contact you within 1 working day. (Please note that our offices are closed over weekends and public holidays.)

Don't Worry

Our privacy policy ensures your data is safe: Incus Data does not sell or otherwise distribute email addresses. We will not divulge your personal information to anyone unless specifically authorised by you.

If you need any further information, please contact us on tel: (27) 12-666-2020 or email info@incusdata.com

How can we help you?

Let us contact you about your training requirements. Just fill in a few details, and we’ll get right back to you.