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

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.

3

u/repeating_bears Jan 21 '26

The IntelliJ message is the same as javac. It presumably just echos it. Yes, it adds a quick fix, but that doesn't mean the message couldn't be better.

6

u/davidalayachew Jan 21 '26

Are they using a decent IDE?

No, and this is a common question/request.

Long story short, we've spent almost a year trying to give students access to tools like IntelliJ, and the result is that they end up being counter productive until the student is at least 6 months into programming. Anything earlier than that, and the tool is a weight around their neck, and the student just devolves into spamming Quick Fix to resolve their problems.

Java isn’t meant to be written in a regular editor

Firmly disagreed. Java is just fine in Notepad++ or vim. I do both all the time. Many of my students (even <=6 months!) did too, with great success.

proper tools make a heck of a difference.

Sure, but see above.

0

u/Hax0r778 Jan 22 '26

So one option helps them too much and the other option helps them too little? I dunno. That seems very arbitrary.

1

u/davidalayachew Jan 22 '26

So one option helps them too much and the other option helps them too little? I dunno. That seems very arbitrary.

One option is painful, but they learn. The other option is painful, and they learn next to nothing. That's the reason why we are so forceful about it.

2

u/philipwhiuk Jan 25 '26

I guarantee they just install it themselves.

I know because it’s what I did at Uni.

The reality is that learning to use an IDE is as important as the language syntax

1

u/davidalayachew Jan 25 '26

I guarantee they just install it themselves.

I know because it’s what I did at Uni.

To be clear, we are not at the stage of "write out this for loop with 3 nested if statements in Notepad++". We are at the stage of "this thing is called a variable, similar to how you see it in Algebra".

I agree that, anything past 6 months, and the ban makes no sense. But if we are trying to teach people how to write if statements and what a class is, then yes, the ban is still useful.

And to be frank, most of these students have never even heard of an IDE before. Most of them have never written a single line of code in their life. Hell, most of them don't even know what a directory is.

I feel like your suggestion assumes that the student has a base level competence with computers in general, and just doesn't know code. That was true 10 years ago, not now.

5

u/0b0101011001001011 Jan 21 '26

That is a shitty defense.

I agree that editors snd better tools make a huge difference, I would not program a single line of java without them.

That still means many language features and default error messages are bad.