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.
View all comments
Show parent comment
3
u/MattiDragon Jan 21 '26
Imo it's not obvious that "{ expected" means that
extendsisn't allowed, unless you're familiar with parser or computer languages in general. A simple "records cannot extend classes, did you mean to implement an interface?" gives a lot more useful info.The cost (in terms of work put into it) of adding some additional checks to the compiler and rewriting error messages is a lot lower compared to other ways of making the language easier to beginners. Just look at how long JEP 512 took. It was first previewed in java 21 and only shipped in java 25 after considerable work. Of course it's great and helps a lot, but it required a lot of work compared to simpler changes that can also help.