sealed classes, records and enums

Sometimes we write something for the book, only to realize it shouldn’t be in the book. This post is an example. Do you think this legal?

public sealed interface MySealedClass permits MyRecord, MyEnum {
}

record MyRecord() implements MySealedClass {}

enum MyEnum implements MySealedClass { VALUE }

It is! A sealed interface can be implemented by any Java type. Which means records and enums are included.

Interesting change in calling methods in Java 17

We have this code in our Java 11 practice exam book. What do you think it prints?

public class Hippo {
    private static void hippo(short num1, short num2) {
        System.out.println("shorts");
    }
    private static void hippo(int... nums) {
        System.out.println("varargs");
    }
    private void hippo(long num1, long num2) {
        System.out.println("longs");
    }
    private void hippo(int num1, int num2) {
        System.out.println("nums");
    }
    public static void main(String... args) {
        hippo(1, 5);
    } }

Reasoning it through, we know, it doesn’t print longs or nums because the main() is static and therefore we can’t be calling an instance methods. The answer is varargs because we have ints. This seems nice and reasonable.

I then ran the same code in Java 17 and it doesn’t compile!

% java17 Hippo.java
Hippo.java:15: error: non-static method hippo(int,int) cannot be referenced from a static context
hippo(1, 5);
^
1 error
error: compilation failed

i do agree that hippo(int, int) can’t be called. I was surprised at the behavior where it no longer compiles. If I remove the two instance variables, Java still prints varargs like I was expecting.

Happy Book Birthday! OCP Java 17 Book Released!

Jeanne and I over the moon to announce the release of our new OCP Java 17 Developer Complete Study Guide! This book is the culmination of years of knowledge that we’ve accumulated writing study guides for Java. It is for anyone who wants to expand their knowledge of Java, or who wants to pass Oracle’s new Java SE 17 Developer 1Z0-829 exam and become a Java 17 Oracle Certified Professional.

Lambdas and streams? Covered! Concurrency, JDBC, and NIO.2? Covered! New Java 17 switch expressions, records, sealed classes, pattern matching? Definitely covered! Modules? Ok, we don’t use them much either, but they are covered for the exam! It’s great for those who are familiar with Java, but want to acquire a deeper understanding of the language. Learn features of the language that will help you write better code!

Our Java 17 book is a streamlined version of our previous books, including all of the new material and changes that you need to know for the exam. Previously, we wrote one book for each of the two exams. Starting with Java 17, there’s only one exam, allowing us to cover the material in a much more concise and straight-forward manner. Put simply, it weighs less than our previous Complete Study Guide!

Want to win a free copy? Our home away from home, CodeRanch, will be running a book promotion next week starting on May 10th!