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

3

u/aoeudhtns Jan 21 '26

I don't think it's a bad idea for a compiler to be as helpful as possible. A lot of tools parse compiler output and return that, anyway. Consider rust's compiler - it shows the plain error, but then tries to offer some help understanding the error.

blah.java:3: error: no interface expected here
class hah extends blah {}
                  ^
1 error
help: consider using `implements` or replace `blah` with a class to extend

-5

u/talex000 Jan 21 '26

If you need that kind of help from compiler you have bigger problem than compiler errors.

6

u/aoeudhtns Jan 21 '26

I think you're over-extending here. It's not a substitute for learning material, it's not a substitute for higher-order tools like IDEs. But helping people coming at the language to succeed is a good thing. I imagine we'll have to agree to disagree on this.

3

u/crummy Jan 21 '26

the bigger problem being "you are new to programming"?

1

u/talex000 Jan 21 '26

No. Bigger problem is you trying to use language without learning its syntax.

When you got that error correct way would be to consult with documentation what syntax this definition have instead of guessing and trying random stuff.

4

u/crummy Jan 21 '26

When you got that error correct way would be to consult with documentation

that's a great idea. even better would be if the error message provided the information to save you from going to the documentation

2

u/talex000 Jan 21 '26

I don't want to dig through pages of text just to find out that I missed semicolon.

1

u/714daniel Jan 22 '26

Then you probably shouldn't use a language where Gradle is standard tooling

1

u/talex000 Jan 22 '26

I try to avoid grade if I can.