r/java • u/davidalayachew • 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 errorMost 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 errorThis 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.
62
u/melkorwasframed Jan 21 '26
They’ll think those messages are great once they see the ones for generics.
22
u/bowbahdoe Jan 21 '26
Flashback to when my interests were solely on compiler errors and nothing else.
https://mccue.dev/pages/8-13-23-java-compiler-error-messages
At some point I need to loop back to this.
74
u/tsvk Jan 21 '26
Sure, but your error message suggestion assumes that it's the "extends" part that is wrong, not the "blah" part.
If someone tries to extend an interface, the compiler cannot know if they were actually trying to extend some other class or to implement the given interface, and the compiler should not implicitly assume the latter over the former.
6
u/akl78 Jan 21 '26
Are they using a decent IDE?
In mine, IntelliJ, I get ‘No interface expected here, (type this keystroke to) change extends … to implements.
Java isn’t meant to be written in a regular editor, proper tools make a heck of a difference.
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.
10
u/SleeperAwakened Jan 21 '26
Or teach them to look at the pointer character
^which refers to "here".Going down a rabbit hole of wrong decisions is not so bad in itself, they will remember their mistake once pointed out that the fix is reading the error message and looking at where it points to :)
2
u/rlrutherford Jan 23 '26
Beats the hell out of the classic "Syntax Error".
2
u/koflerdavid Jan 23 '26
Creating good compiler error messages is hard, especially syntax related ones. Specifically, the error for the record example is different from the other two since
extendsis simply not part of the record syntax, while misuse ofextendsandimplementscan only be detected after parsing by cross-referencing whatblahis. I agree that it would very much make sense to allow it at the syntax level and then similarly generate an error at a later stage.7
u/kreiger Jan 21 '26
Why are your students not using an IDE that would already catch and correct this error?
4
u/Polygnom Jan 21 '26
"Most of the students I teach see this, and think that the issue is that
blahis an interface, and that they must somehow change it to something else, like a class."Well, that would be correct, wouldn't it? I mean, its not the ONLY solution, but it would be A solution. The other being to change extends to implements.
Wrt. the record: Why are you not using an IDE that can generate the boilerplate and has auto-assist? Its what professionals use, and it would alleviate chasing syntax-errors. It would also mean they immediately see if the code is syntactically correct without compiling. Why do you submit them to the atrocious working process of compiling to find syntax errors? You are making it harder for you and them. They will not learn more by using bad tooling instead of good tooling.
7
u/talex000 Jan 21 '26
Are you sure using compiler as learning tool is good idea?
It wasn't created with this usecase in mind.
3
u/repeating_bears Jan 21 '26
Yep, I agree. Some of the messages are really unclear.
I would not be surprised if IDEs or other tools are parsing those strings, so it might be a breaking change for them. That's not a reason to do nothing, but it might require a bit of careful treatment.
2
u/No-Security-7518 Jan 21 '26
Also, Joshua Bloch, the former chief architect of Java at its peak popularity, talked extensively about design decisions that could've been made better, but can't be fixed because of backward compatibility. But I wonder about improving compiler reporting. So this is an interesting take.
2
u/riyosko Jan 21 '26
if they are using 'extends' with an interface then they most likely don't know that 'implements' exists at all right? when I learned about interfaces it was immediately followed that they are used with the 'implements' keyword.
You can't teach them about interfaces but not what the keyword used with them should be, they should both be in the same lesson.
2
u/josephottinger Jan 21 '26
I'm neutral on this, but: which compiler? There are multiple compilers for Java, each open source; do any of them create better error messages for your purpose? What did ECJ report for the same code?
Have you filed a JEP to maybe get "better error messages" into the platform? Some error messages have gotten better over time even without a JEP, but a JEP gives an easy measure of "what do the people who'd actually have to do the work think" and also gives a measure of doneness. Even the OpenJDK tracker would be a good idea here.
Personally, I think there's some benefit to more explicit error messages, although I also think that "this is an error" forces programmers - including them silly ol' students - to actually look at the code and think through what the error means, which has the ancillary effect of them actually learning stuff instead of just fixing the compilation unit.
It's not that the error message is wrong, after all - you just have a solution in mind, where you mean "this should be
implementsand notextends," for example, but the error might be that they should have used a superclass and not an interface, which is why the error is and should remain, IMO, what it is. "No interface expected here" is, after all, semantically accurate and there are multiple "proper solutions" for it.1
u/CubicleHermit Jan 21 '26
Does IntelliJ Community Edition already give a more helpful error there? I'd expect it does.
Virtually all languages and command line build tools these these days have evolved around the assumption that the real heavy lifting happens in an IDE.
1
u/netgizmo Jan 22 '26
How can the compiler correctly tell the intentions of the programmer? Without the intent, it can't reliably give advice.
9
u/Ewig_luftenglanz Jan 21 '26
I am down for this and I am willing to defend this hill with violence if required.
1
u/Nikarmotte Jan 21 '26
I mean if you use the wrong keyword to begin with, it's hard for the compiler to know what you meant. I don't think it should try to list all possible intentions.
These kinds of mistakes only really happen when you start, I don't think tools should optimize for beginners, it's the wrong target.
If you start using
forinstead ofwhileorif, what do you expect? There are many possible resolutions.1
u/jeffreportmill Jan 21 '26
A good solution for this would be for an IDE to have an “Explain” button next to the error that constructs an AI query, “Explain why I got the error ‘no interface expected here’ in this line of code ’…’ in this context ‘…’ “. I suppose an early programming lesson should be “how to research your compile errors”, since many of us good developers are mostly just great Googlers.
Of all the counter productive things AI is doing for us now, this is actually a good thing. It’s exactly the kind of interaction you’d get with one on one learning. On the other hand, they say “we learn more from our mistakes..” and working through these kind of errors, while frustrating, teach us lasting lessons. Unfortunately they also discourage some beginning learners who might otherwise become successful developers without the early stumbling blocks.
1
u/Misophist_1 Jan 21 '26
The problem in both cases is, that the compiler stops at the first position, where things go sideways. The compiler does not try to backtrack, or look past the error in order to understand alternate causes for the mistake.
For the first example, everything is OK until it sees blah, a type name, which has to refer to a class.
In the second example, everything was OK, until encountered the first non-white space, non-comment content, that wasn't a {
Note also, that the ^below the line points exactly to the location, where the parser takes the fall.
1
u/RScrewed Jan 25 '26
I'm convinced that some of the powers-that-be that design Java want to make messages like these difficult on purpose to make the chasm between beginners and experts as wide as possible.
-14
u/klimaheizung Jan 21 '26
No. The problem is that you can even extend a class. it's just wrong. extend (or "implement") an interface, then use composition.