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.

98 Upvotes

180 comments sorted by

View all comments

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 implements and not extends," 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.

9

u/repeating_bears Jan 21 '26

"Multiple compilers" - I mean realistically there are 2, and if we're talking about just one, we all know which one he's talking about.

A JEP is very likely not the right process for such a change, and anyway, what's wrong with having a discussion before directing something to the formal channels?

0

u/josephottinger Jan 21 '26

On "multiple compilers," sure. We can easily presume he's talking about javac. But ... are the error messages part of the specification? Is the compiler part of the specification? (It may be: I don't think it is. And cursory investigation to check either and both of those questions suggests that no, neither the compiler nor the error messages are required to be what they are.) If you want a feature X, and feature X is available from a means, then use whatever that is if it suits your purposes.

And as far as "what's wrong with..." nothing. Have the discussion. Heck, I'll participate if I think I have anything worth adding to it.

2

u/davidalayachew 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?

Hmmm, good point. Maybe that's a failure in our decision-making.

We used OpenJDK, and we did consider ECJ, but not seriously. Maybe we should have, but it just didn't seem that much better.

It's not that the error message is wrong, after all - you just have a solution in mind, where you mean "this should be implements and not extends," 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.

Yes, my example is a bit poor. I was more trying to give a more neutral message of what is wrong, but yes, it did sort of morph into a suggestion.

Maybe we could find the few common tripping points, and then have those errors special cased to include common suggestions? Still not sure on the solution.

2

u/josephottinger Jan 21 '26

I would 100% hesitate to say you've had a failure in your decision-making. I don't think you have. I don't know what the conclusion should be, obviously, but I don't think you're necessarily barking up any unnecessary trees, as the saying I just made up goes.

And to be clear, I don't KNOW that ECJ has better error messages, or worse; it's just that THE COMPILER is replaceable in the Java ecosystem, as long as it generates valid code, and thus the error messages are also mutable.

I don't know the ideal solution, either - I'm very old school, so I'm like "let 'em sink or swim," but what's computing power for if not to make our lives easier?

2

u/davidalayachew Jan 21 '26

I don't know the ideal solution, either - I'm very old school, so I'm like "let 'em sink or swim," but what's computing power for if not to make our lives easier?

That's the environment I was raised in (and presumably, most of this comment section as well), so I can relate.

Sadly, that is NOT AT ALL representative of our current student body.

There are a number of students who struggle to understand the file system and how directories work, as they are used to working with phone applications, which rarely if ever work with folders directly.

These are the crop of students we are working with. And for better or worse, it is our job to show them the true nature of programming, and if they are willing, then to nurture them into programmers.

Worse yet, these kids are ignorant, but not stupid. There are some from this bunch that didn't even KNOW what a folder was, but quickly became not only the most competent in their class, but among the most competent I have met since I start tutoring in 2012.

So it's not something inherent, it's just that their experience growing has basically knee-capped them, and we're the ones that have to foot the bill. Once you get them on their feet, they move quick. If we're going to talk about inherent traits of this generation, I'd say they have less problem-solving skills, but can think way faster than the previous generation.

1

u/philipwhiuk Jan 25 '26

I mean if they don’t understand computers at all it’s extremely stupid that they should pass the course.

Sounds like you need better prereqs

2

u/davidalayachew Jan 25 '26

I mean if they don’t understand computers at all it’s extremely stupid that they should pass the course.

Sounds like you need better prereqs

Lol, we are the pre-requisite. I have been talking about the equivalent of CS 101.