r/java • • Jan 21 '26

Java compiler errors could be more intelligent

I tutored many students over the past several years, and a common pain point is the compiler messages being misleading.

Consider the following example.

interface blah {}
class hah extends blah {}

When I compile this, I get the following message.

blah.java:3: error: no interface expected here
class hah extends blah {}
                  ^
1 error

Most of the students I teach see this, and think that the issue is that blah is an interface, and that they must somehow change it to something else, like a class.

And that's still a better error message than the one given for records.

blah.java:2: error: '{' expected
        public record hah() extends blah {}
                           ^

This message is so much worse, as it actually leads students into a syntax rabbit hole of trying to add all sorts of permutations of curly braces and keywords, trying to figure out what is wrong.

If we're talking about improving the on-ramp for learning Java, then I think a core part of that is improving the error --> change --> compile feedback loop.

A much better error message might be this instead.

blah.java:3: error: a class cannot "extend" an interface, only "implement"
class hah extends blah {}
                  ^
1 error

This is powerful because now the language grammar has a more intelligent message in response to an illegal (but commonly attempted) sequence of tokens.

I understand that Java cannot special-case every single illegal syntax combination, but I would appreciate it if we could hammer out some of the obvious ones. extends vs implements should be one of the obvious ones.

97 Upvotes

180 comments sorted by

View all comments

5

u/wildjokers Jan 21 '26 edited Jan 21 '26

This seems reasonable to me, you might want to bring this up on an openjdk mailing list. As the changes JDK devs made for the instance main methods (JEP 445) show they are open to helping lower barriers for students.

5

u/davidalayachew Jan 21 '26

This seems reasonable to me, you might want to bring this up on an openjdk mailing list. As the changes JDK devs made for the instance main methods (JEP 445) they are open to helping lower barriers for students.

Thanks, will do. I wanted to float it here first, just to get a lay of the land. Seems the consensus is mildly positive, but most people are wondering why not just use the IDE to close the gap. The problem is that major IDE's overwhelm completely new students (<= 6 months), and they tend to stop learning and spam Quick Fix. So, just throwing IntelliJ at them isn't a great solution, from our experience.

4

u/wildjokers Jan 22 '26

Seems the consensus is mildly positive

It would be more positive if people would remember how they felt when they were starting out.

2

u/davidalayachew Jan 22 '26

It would be more positive if people would remember how they felt when they were starting out.

My reading from the commentors thus far is that, they actually do remember what it was like to be the brand new programmer. The problem is, they were the ones who swam in the "sink or swim" situation. So, I think they aren't doing a great job of putting their brains into the perspective of a sinker. I had to explain to someone that most non-technical people might interpret "{ expected" as a suggestion to insert a curly brace there, and they reacted to my comment with surprise and confusion. To give one example.

1

u/experimental1212 Jan 23 '26

It's a great opportunity to get the student to see it from the parser's perspective. "I want to drink a....chicken." Chicken is unexpected because we assume everything up to that point makes sense. I don't want the parser telling me you can replace "drink" with "eat". There are so many other things you can do with a chicken....we don't know what is "correct" but we do know what is unexpected.

1

u/davidalayachew Jan 23 '26

It's a great opportunity to get the student to see it from the parser's perspective. "I want to drink a....chicken." Chicken is unexpected because we assume everything up to that point makes sense. I don't want the parser telling me you can replace "drink" with "eat". There are so many other things you can do with a chicken....we don't know what is "correct" but we do know what is unexpected.

I'd agree with all of this if the compiler said something like "extends" not expected here. But instead, it said { expected. That's just a bad message. To use your analogy, it'd be like if the compiler said "water expected". By all means, water is the most likely followup to that message, but it's certainly not the only one.

2

u/experimental1212 Jan 23 '26

Ah you're right, great point.