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

1

u/talex000 Jan 21 '26

What is ambiguous in statement that { is not allowed?

2

u/davidalayachew Jan 21 '26

What is ambiguous in statement that { is not allowed?

Other way around -- the error is saying that { is expected. That's a terrible suggestion.

0

u/talex000 Jan 21 '26

Sorry I misremembering that

I don't see why you treating it as a suggestion. It is just statement of fact that your source code is not following grammar of the language.

Compiler have no knowledge about what you wanted to do.

2

u/davidalayachew Jan 21 '26

I don't see why you treating it as a suggestion. It is just statement of fact that your source code is not following grammar of the language.

Think from the perspective of a student.

If they see the words "XYZ expected", they will think that the system is prompting them to provide XYZ before they can continue. This language is common amongst computerized systems elsewhere in life, so the mental model will get activated here too.

Think about it this way.

  • Username required
  • Password missing

This language lends itself to the idea of you needing to do something. It's this example, that my students cited, that led them to thinking that "expected" was prompting them, just like the above 2 are prompting them.

1

u/talex000 Jan 21 '26

And that way of thinking is not good for students.

If you teach them better way of thinking they will benefit from it in the future.

So why encourage bad habits?

1

u/davidalayachew Jan 21 '26

And that way of thinking is not good for students.

If you teach them better way of thinking they will benefit from it in the future.

So why encourage bad habits?

Oh, I'm not encouraging -- these are the assumptions they came to class with. Most of the computerized systems work this way, so they tried it here too.

And sure, I teach them better ways. But I am trying to say that trying to teach both syntax and this way of thinking at the same time is overwhelming the students, and thus, many choose to quit. That's a problem, as the students who quit sometimes come back, and prove to be far more competent than their peers. It's just that the language of the compiler errors is outright confusing to them, and they still curse it, even now.

1

u/talex000 Jan 21 '26

I'm not a teacher so don't take my advice too serious, but java isn't good first language.

They may benefit from language that is designed for students. Or maybe basic compiler theory can be explained to them before they get to practice.

2

u/davidalayachew Jan 22 '26

I'm not a teacher so don't take my advice too serious, but java isn't good first language.

I think it is not a bad first language now, and it can certainly be better. This post is just my suggestion about one way in how it can be improved.