Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

568
Views
Java: Allowed statements after labels

I'm playing around with Java Syntax, so this is question arises purely from curiosity. This piece of code:

http://www.google.com
Object val = 5 <- 4;

does not compile, because a label (http) "must be followed by a statement". The following two variants do compile:

http://www.google.com
{ Object val = 5 <- 4; }

and

Object val;
http://www.google.com
val = 5 <- 4;

In both cases, I switched from a declaration to an expression. This makes me wonder what exactly is a "statement" in Java, but the doc states:

In addition to expression statements, there are two other kinds of statements: declaration statements and control flow statements. A declaration statement declares a variable.

The JLS just says (on labels) that

The Identifier is declared to be the label of the immediately contained Statement.

It does not say anything about "expression statements".

Did I miss something, or is this just an unclear/incorrect specification?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

If you read chapter 14 of the JLS a bit more carefully, you'll find that a LocalVariableDeclarationStatement is not a Statement. Not very intuitive of them, is it?

Specifically, in JLS 14.2, we see that:

  • a Block essentially consists of zero or more BlockStatements
  • a BlockStatement is one of:
    • LocalVariableDeclarationStatement
    • ClassDeclaration
    • Statement

So a LocalVariableDeclarationStatement is not a descendant of Statement in the hierarchy, but rather a sibling. They are both types of BlockStatements.

A label must be followed by a true Statement — that is, the specific subtype of BlockStatement that is neither a LocalVariableDeclarationStatement nor a ClassDeclaration. The various subtypes of Statement are listed in 14.5. You will not find LocalVariableDeclarationStatement among them, though you will find ExpressionStatement as a subtype of StatementWithoutTrailingSubstatement.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!