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.

95 Upvotes

180 comments sorted by

View all comments

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.

36

u/cowwoc Jan 21 '26

The error sentence could continue by saying "either change extends to implements or replace the interface by a class."

A lot of times what is missing is not explaining what is wrong but rather how to fix it. 

35

u/plumarr Jan 21 '26

I would be careful with error message that give an advice on how to solve the issue because if the proposed solution is incorrect, it's also a way to send the user down the rabbit hole. 

instead it should just clearly describing the error and eventually giving a reminder of the rules. 

So in the current case, we could just use "A class can either extends another class or implements an interface", without suggesting what the user should do. 

You may think that's equivalent to your proposition, but it isn't. What you propose doesn't offer a solution if "extends blah" is correct and what is incorrect is that"blah" as been declared as an interface and not a class.

5

u/anyOtherBusiness Jan 21 '26

“Or by declaring an interface in the first place” you know, interfaces can extend other interfaces. Now there are three possible solutions. The compiler has no way of knowing the actual intention.

3

u/sam123us Jan 21 '26

I agree with this suggestion. Lately I have had to work with python and I am newbie in python so make silly mistakes. The python interpreter tells me options. I can see if the suggestion makes sense but has saved me lot of time

1

u/bowbahdoe Jan 21 '26

Right and that's a categorically different bit of information. Hints and context should be different and not munged haphazardly into the error text

1

u/bichoFlyboy Jan 23 '26

You can also replace the class by an interface.

You can do:

interface MyParent{}
interface Child extends MyParent{}

3

u/davidalayachew Jan 21 '26

Sure, but your error message suggestion assumes that it's the "extends" part that is wrong, not the "blah" part.

True, my message could stand to be more impartial. Maybe it should instead highlight both possibilities. Obviously, it doesn't scale, but I don't think it needs to. It just needs to be able to cover the most common tripping points. Everything else, we can ignore/leave default.

1

u/grimonce Jan 22 '26

It can provide a hint, are you trying to implement an interface?

There's only so many options one can try with this keyword.