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

Show parent comment

9

u/repeating_bears Jan 21 '26

"Multiple compilers" - I mean realistically there are 2, and if we're talking about just one, we all know which one he's talking about.

A JEP is very likely not the right process for such a change, and anyway, what's wrong with having a discussion before directing something to the formal channels?

0

u/josephottinger Jan 21 '26

On "multiple compilers," sure. We can easily presume he's talking about javac. But ... are the error messages part of the specification? Is the compiler part of the specification? (It may be: I don't think it is. And cursory investigation to check either and both of those questions suggests that no, neither the compiler nor the error messages are required to be what they are.) If you want a feature X, and feature X is available from a means, then use whatever that is if it suits your purposes.

And as far as "what's wrong with..." nothing. Have the discussion. Heck, I'll participate if I think I have anything worth adding to it.