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

4

u/Polygnom Jan 21 '26

"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."

Well, that would be correct, wouldn't it? I mean, its not the ONLY solution, but it would be A solution. The other being to change extends to implements.

Wrt. the record: Why are you not using an IDE that can generate the boilerplate and has auto-assist? Its what professionals use, and it would alleviate chasing syntax-errors. It would also mean they immediately see if the code is syntactically correct without compiling. Why do you submit them to the atrocious working process of compiling to find syntax errors? You are making it harder for you and them. They will not learn more by using bad tooling instead of good tooling.

4

u/davidalayachew Jan 21 '26

Well, that would be correct, wouldn't it? I mean, its not the ONLY solution, but it would be A solution. The other being to change extends to implements.

Right. My criticism is more that the error, as is, implies that that is the issue. I feel it could either be less "biased" (for lack of a better word), or try and give suggestions. I'm not married to either idea, I just think that that isn't very good as is.

Wrt. the record: Why are you not using an IDE that can generate the boilerplate and has auto-assist? Its what professionals use, and it would alleviate chasing syntax-errors. It would also mean they immediately see if the code is syntactically correct without compiling. Why do you submit them to the atrocious working process of compiling to find syntax errors? You are making it harder for you and them. They will not learn more by using bad tooling instead of good tooling.

This is a common question/criticism that many developers give.

Respectfully, whenever I hear this, I wonder if the person saying it remembers what it was like being this inexperienced.

To answer your question, I've spent over a decade attempting exactly this suggestion, and the answer is that it does not work.

The students are already completely overwhelmed by the details about syntax and logic and organization and holding state in their head. Every single time, they just end up getting crushed by all the other details about the IDE, and their brains just shut down and spam Quick Fix.

It's this exact same mentality that leads students to just jump straight to AI to get past the obstacle, as opposed to understanding the problem itself.

So yes, we actively discourage students from trying to reach for something like IntelliJ in the first few months of learning programming. After that, it's basically up to the student. At least by then, they have the fundamentals down.

1

u/Hax0r778 Jan 22 '26

Every single time, they just end up getting crushed by all the other details about the IDE, and their brains just shut down and spam Quick Fix.

Not all IDEs have a Quick Fix button. Use a simpler IDE like Blue Jay or Netbeans which helps with syntax but doesn't auto-generate all the code. None of your IDE objections seem to apply to IDEs in general. They mostly seem aimed at IntelliJ?

1

u/davidalayachew Jan 22 '26

Not all IDEs have a Quick Fix button. Use a simpler IDE like Blue Jay or Netbeans which helps with syntax but doesn't auto-generate all the code. None of your IDE objections seem to apply to IDEs in general. They mostly seem aimed at IntelliJ?

Well, aimed at ones like Eclipse/IntelliJ/VSCode/etc.

BlueJ is a much better suggestion, and we are actually strongly considering it (I am more partial to jGRASP), but our results thus far show that it doesn't help much and it kind of hurts, as visual density is surprisingly important for brand new students (and judging solely by that metric, cli is clearly the winner).

We probably will end up on something like jGRASP or BlueJ. Not decided yet.

1

u/Polygnom Jan 21 '26

"Respectfully, whenever I hear this, I wonder if the person saying it remembers what it was like being this inexperienced."

Yes, I remember perfectly. We started out with BlueJ. Which was already better than using the javac directly, and had the advantage of being able to run static methods directly.

I quickly ditched that and used Eclipse, and felt ridiculous for our teacher not telling us about it way earlier. (IntelliJ did not even exist back then).

"The students are already completely overwhelmed by the details about syntax and logic and organization and holding state in their head. "

I feel like they would grasps concepts better if syntax wasn't an issue. Lets face it, we rarely think about syntax, and can easily grasp new syntax because we know the concepts. I feel like learning the concepts is way more important than making them fiddle with syntax.

I don't know how to best remove fiddling with syntax unnecessarily, but having them compile manually isn't it. Maybe IDEs are the solution, maybe they aren't, but I don't see a better way except starting with something entirely different like Scratch.

1

u/davidalayachew Jan 21 '26

Yes, I remember perfectly. We started out with BlueJ. Which was already better than using the javac directly, and had the advantage of being able to run static methods directly.

I quickly ditched that and used Eclipse, and felt ridiculous for our teacher not telling us about it way earlier. (IntelliJ did not even exist back then).

I'm glad your experience was so positive. There are a few students who can echo your experience. The overwhelming majority would say almost the opposite of you.

Mind if I ask what year you started learning programming? I want to test an unfounded suspicion of mine.

I feel like they would grasps concepts better if syntax wasn't an issue. Lets face it, we rarely think about syntax, and can easily grasp new syntax because we know the concepts. I feel like learning the concepts is way more important than making them fiddle with syntax.

I don't know how to best remove fiddling with syntax unnecessarily, but having them compile manually isn't it. Maybe IDEs are the solution, maybe they aren't, but I don't see a better way except starting with something entirely different like Scratch.

Oh, we already worked around it, but in a super unideal way.

Long story short, we basically did exactly that -- created our own layer on top of Java to make things easy to learn the basic concepts of. It's basically a Java version of Karel the Robot. So far, this has been the solution that gives us the most success. But it still feels like hitting a speed bump going 55 mph when we wean them off of it to just writing Java. Still, way better than starting them off on IntelliJ from the start.

2

u/Polygnom Jan 22 '26

Well, BlueJ was released in 1999 and IntelliJ in 2001. Actually, this makes me incorrect. IntelliJ did just exist when I started programming, but I didn't know about it.

I don't know what kind of assumption you have, but kids back then were not smarter than kids today.

1

u/davidalayachew Jan 22 '26

I don't know what kind of assumption you have, but kids back then were not smarter than kids today.

But they were significantly less distracted. Kids these days have to deal with social media, less freedom to roam outside, increased wariness of strangers, etc. In short, there is less patience in this batch of kids compared to the ones before. And I have been teaching kids for about a generations length, so I have been able to see the changes happen live.

-2

u/repeating_bears Jan 21 '26

Altering the interface to be a class is a far less likely solution.

You necessarily wrote (or have been provided) the interface first, before erroneously adding it to the extends clause. Someone - either yourself or the author - put some degree of thought into what kind of type it should be.

It's far more likely to simply mix up the keywords than it is to realise when you're writing the class that you fundamentally misdesigned the interface, and that that should have actually been a class.

In any case, the error message they suggested as an improvement does not preclude changing the interface. The current error message outlines an expectation, but it misses the context of why it made that expectation.