I’ve been programming in Java for a while, and I’ve just come across this syntax for the first time:
public Object getSomething(){return something;};
What’s interesting me is the final semicolon. It doesn’t seem to be causing a compiler error, and as far as I know isn’t generating runtime errors, so it seems to be valid syntax. When would I use this syntax? Or is it just something that is allowed but generally not used?
It’s allowed by the grammar as a concession to harmless syntax errors, but it’s not generally used and doesn’t mean anything different (than leaving the semicolon out).
Just as a
};inside a method (such as after anifblock) is a null statement and is allowed, an errant semicolon outside a method is considered a null declaration and is allowed.Specifically, the following production from the Java Language Specification allows this: