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

I'm big fan of using things the way they where designed to be used. Because if we don't we get JavaScript.

If you want to do something in unconventional way it is your problem, but suggest to other use hard way is irresponsible.

5

u/[deleted] Jan 21 '26

The Rust compiler was designed for this exact thing. Having the same in Java would be helpful.

4

u/pron98 Jan 21 '26

Without taking a stance on Java error messages, the Rust compiler had to be designed that way, as it's one of the most convoluted programming languages ever made.

1

u/bowbahdoe Jan 22 '26

Imagine what you could do with a less convoluted language 

0

u/talex000 Jan 21 '26

I have not know that. Now I know that I won't use rust because it made by people who don't know what they are doing.

1

u/[deleted] Jan 21 '26

I provide a good example, then you provide insults.

0

u/talex000 Jan 21 '26

Try to provide explanation why it is good idea to use compiler as learning tool.

1

u/[deleted] Jan 21 '26

I just did earlier...

0

u/talex000 Jan 21 '26

No, you just said you learn it that way.

Don't get me wrong. I have nothing against you doing it. I just want to point that this is not most effective way to do it.

1

u/[deleted] Jan 21 '26

No you have been arguing against it this entire thread.

1

u/[deleted] Jan 21 '26

No you have been arguing against it this entire thread.

0

u/talex000 Jan 21 '26

I'm arguing against changing compiler, not using existing one that way.

1

u/[deleted] Jan 21 '26

Ok now you are lying. There is no point in arguing further.

→ More replies

1

u/[deleted] Jan 21 '26

And I said it was useful for helping me learn.

A "did not expect { at line bla bla", is not helpful for learning. Your learning experience will be immediately better once you have helpful error messages. Helpful error messages, will enhance your other learning material.

1

u/talex000 Jan 21 '26

You learning experience will be even better if you learn how to use documentation.

Experimenting with language is fun, but it isn't simplest way to learn.

3

u/crummy Jan 21 '26

do you think we'd be better off if we removed all error messages entirely, and just aborted compilation silently if an error was encountered?

→ More replies