Skip to content

JEPs to Simplify Teaching Java to Students

barryburd edited this page Feb 6, 2025 · 35 revisions

As we prepare to celebrate 30 years of Java technology in 2025, it’s time to pass the baton to the next generation. The Java in Education initiative looks towards Java community leaders to inspire their local community of junior developers and students to learn and use Java technology.

In the April 2024 JCP Executive Committee meeting, we discussed several Java enhancement proposals that are being developed in OpenJDK that help to support the initiative of bringing Java to the next generation of developers, making it easier to teach and to learn Java.

Some of the challenges in teaching Java to students and more junior technical professionals include Java's verbose "Hello World!" program, having access to machines that can have Java installed (Chromebooks, for example), and older curricula that don’t incorporate more modern Java features.

To specifically address the onramp of learning and teaching Java to students, this article will summarize some of the Java enhancement proposals that are currently being developed in several OpenJDK projects.

We will focus on seven JEPs (Java Enhancement Proposals) in various stages of development in OpenJDK:

JEP 222— jshell: The Java Shell (Read-Eval-Print Loop)
JEP 330— Launch Single-File Source-Code Programs
JEP 458— Launch Multi-File Source-Code Programs
JEP 445— Unnamed Classes and Instance Main Methods (Preview)
JEP 463— Implicitly Declared Classes and Instance Main Methods (Second Preview)
JEP 476— Module Import Declarations (Preview)
JEP 477— Implicitly Declared Classes and Instance Main Methods (Third Preview)
JEP 494— Module Import Declarations (Second Preview)
JEP 495— Simple Source Files and Instance Main Methods (Fourth Preview)

Below are the parts of a program for beginners to notice when they start learning Java:

image

Java's introductory tutorials typically start with disclaimers about class, public, static, System, and other aspects of a "Hello World!" program. A student's first exposure to Java is like clicking a license agreement's Accept button. Most people say, “The license agreement is too long and complicated, so I'll just click the Accept button and hope for the best.” In the same way, Java instructors will often tell students to “Ignore those parts of the program. We'll explain them later. ”

One of Java's underlying principles is the notion that every function belongs inside of a class. Even main – a program's starting point – is a method within a class. And you don't automatically have an instance of that class, so the main method has to be static.

The problem with this is that beginners need time to learn about object-oriented programming. What's second nature for experienced Java developers is mystifying for novices. Beginners don't understand how classes work until they write several programs in a language like Java. We can explain the word static several times in several different ways, but students don't understand static until they feel compelled to use it.

A simple programming environment

With the implementation of JEP 222 in 2017, Java 9 included JShell — an environment for testing Java code snippets. JShell gives the programmer instant feedback and eliminates the need to create classes and methods. Here's a brief session in the JShell console:

$ jshell
| Welcome to JShell
| For an introduction type: /help intro

jshell> System.out.println("Hello World!")
Hello World!

jshell> 17 * 259.03
$2 ==> 4403.509999999999

jshell> "Hello, "
$3 ==> "Hello, "

jshell> + "friend!"
$4 ==> "Hello, friend!"

jshell> /exit
| Goodbye
$

In this session, the "Hello World" snippet is only one line of code. For the most part, semicolons are optional. The environment even accepts simple expressions and displays their values without the need for a println call.

In October 2023, Oracle announced a new Java extension for VS Code. The extension includes features such as autocompletion, error highlighting, hover documentation, code formatting, refactoring, and more. By adding Senthilnathan's JShell Easy extension to VS Code, a session takes on a more modern look and feel.

image

A leaner version of “Hello World!”

In September 2022, a document by Brian Goetz suggested ways to trim the boilerplate code from Java's simplest programs. As always, the prime directive was to avoid breaking the language's well-established rules. So having main live outside of a class wasn't part of the plan. Instead, the core idea was to add an implicitly declared class feature to Java. From Java's early days, the unnamed package has been implicitly declared, so the new implicitly declared class idea wasn't a shock to the system.

With the release of Java 21 in September 2023, implicitly declared classes and what would eventually be called instance main methods entered the language as first-time preview features.

Let’s look at an example.

Picture2

With the ability to launch single-file source-code programs (JEP 330) you could issue this command:

java --enable-preview --source 21  Hello.java

The result was the expected Hello World! output. This less verbose program made use of two Java 21 preview features, both in JEP 445.

  • A file that contains a main method doesn't have to explicitly declare a class. Java automatically wraps the file's contents in an implicitly declared class.
  • A program's main method can be parameterless and doesn't have to be public or static.

The fine print

You can use these two features independently of one another. For example, the following file has an instance main method without an implicitly declared class:

class Hello2 {
    void main() {
        System.out.println("Hello World!");
    }
}

And this file has an implicitly declared class without an instance main method:

static String greeting = "Hello World!";

public static void main(String[] args) {
    System.out.println(greeting);
    new MyInnerClass();
}

static class MyInnerClass {
    MyInnerClass() {
        System.out.println("I'm an inner class!");
    }
}

In the previous example, everything in the file gets wrapped inside an implicitly declared class. The class has three members – greeting, main, and MyInnerClass.

It's important to remember that a file's outermost scope must have a main method in order to have an implicitly declared class. For example, the following file's code doesn't compile:

// THIS FILE DOESN'T COMPILE

static String greeting = "Hello World!";

public static void doSomething(String[] args) {
    System.out.println(greeting);
}

And neither does this file's code:

// THIS FILE DOESN'T COMPILE

static String greeting = "Hello World!";

static class MyInnerClass {

    public static void main(String[] args) {
        System.out.println(greeting);
        new MyInnerClass();
    }
}

Brian Goetz's original proposal referred to this type of class as an unnamed class. There's only so much you can do with a class that doesn't have a name. Imagine a directory containing two files. The first file is named MyClass.java:

// MyClass.java
void main() {
    new Other();
}

The second file is named Other.java.

public class Other {

    public Other() {
        System.out.println("I'm an Other class instance.");
    }
}

With the delivery of JEP 458 and JEP 463 in Java 22, you can compile and run these two files by typing one command:

java --enable-preview --source 22  MyClass.java

This java command doesn't put any .class files on your hard drive. With the java command, the MyClass.java file's implicitly declared class doesn't have to have a name. But you can also use the tried-and-true javac command to compile these two files:

javac --enable-preview --source 22  MyClass.java

When you do, you get two new files named Other.class and MyClass.class. The implicitly declared class takes its name from the base name of its .java source file.

Remember that implicitly declared classes and instance main methods are Java 21 preview features. You cannot use these features in production-level code. These features may be modified or even removed completely in future versions of Java.

JEPs 476 and 477: Can we do even better?

More features aimed at new programmers are being developed for the next few Java releases. Take a look at the following Welcome.java file:

import java.util.Scanner;

public class Welcome {
    public static void main(String[] args) {
        var scanner = new Scanner(System.in);
        System.out.print("What's your name? ");
        var name = scanner.nextLine();_**
        System.out.println("Hello, " + name + "!");
    }
}

When we run this code, this is the result:

What's your name? Barry Burd<BR>
Hello, Barry Burd!

The Java features in this code aren't new. The restricted keyword var was introduced as part of Java 10, and the Scanner class dates all the way back to Java 1.5. Before Java 1.5, teaching console input to new Java programmers was much more complicated.

We can edit the Welcome.java file so that it uses Java 21 preview features. When we do, we get the following file:

import java.util.Scanner;

void main() {
    var scanner = new Scanner(System.in);
    System.out.print("What's your name? ");
    var name = scanner.nextLine();
    System.out.println("Hello, " + name + "!");
}

Using the preview features in Java 21, we've made the Welcome.java file's code less verbose. But with features described in JEPs 476 and 477, we can do even better. Here are three of the features described in the JEPs:

  • Add the ability to import all the classes in a module with a single import declaration.
  • Automatically import the java.base module in all implicitly declared classes.
  • Add an easy-to-use class, named java.io.IO, to the java.base module.

With the first of these features, any file can begin with a line such as:

import module java.base;

This is useful even for seasoned developers when they are writing throw-away trial code. But, when you combine the second and third features, there is an automatic import of java.io.IO.

The proposed java.io.IO class has three static methods – two for writing to the console (println and print) and a third for prompting and reading from the console (readln). New programmers don't have to instantiate Scanner and wonder what System.out means. The resulting Welcome.java file looks like this:   Picture3

The message that a newcomer will see when they start learning Java is much more motivating compared to the older Welcome.java file:

image

The preview features in Java 21 and beyond make it easier for beginners to start learning Java. The newest proposal, Simple Source Files and Instance Main Methods (Fourth Preview), makes a slight change in terminology with implicitly declared class replaced by the name simple source file.

The nine JEPs being developed by OpenJDK contributors along with feedback from Java community members and leaders can help to inspire the next generation of Java developers. These new features will make it easier for educators to teach Java and spread the Java in Education initiative.

Whether you are a new student just learning Java or an experienced expert, these new developments will increase your joy in writing Java programs. We encourage you to share these new features and how you implement them to raise awareness within your community.

Clone this wiki locally