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 String
s, 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 thepublic
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 thedefault
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 thepackage
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 frommain()
, then they have to mark itstatic
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!