• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

labels

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Why does this compile


but this result in a compilation error:



Okay, one is an assignment, but they're both a statement, no? There's some subtlety here that I'm missing.

Thanks.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's kind of hard to say without seeing the rest of the code, and what the EXACT compilation error is.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In all the years I've used Java (and it's a lot of years) I have never used a label so can't be certain on this but I suspect the label must be followed by an assignment or a statement and the second one is a declaration which isn't allowed.
 
Steven Squeers
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry; the following code:



generates the following error:

C1.java:5: error: '.class' expected
int i=10;
^
C1.java:5: error: not a statement
int i=10;
^
C1.java:5: error: illegal start of expression
int i=10;
^
C1.java:5: error: ';' expected
int i=10;
^
4 errors
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Squeers wrote:
Why does this compile


but this result in a compilation error:



Okay, one is an assignment, but they're both a statement, no? There's some subtlety here that I'm missing.




Short answer. It is defined that way in the Java Language Specification. Longer answer, as to why it is defined that way -- not sure, perhaps the designers forgot about that case. There are just so many statement types, I am not surprised that the case is missing.

Henry
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So here is my read on why it is illegal:

According to JLS (here: http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html):

14.4 Local Variable Declaration Statements wrote:Every local variable declaration statement is immediately contained by a block.


While

14.7 Labeled Statements wrote:A labeled statement is executed by executing the immediately contained Statement.



A local variable declaration must be in a block. A labeled statement is not a block, but contains the statement that is part of the labeled statement. So a local variable declaration after a label would be a variable declaration not immediately contained in a block.

The error messages I see:

Are a bit non-expressive, but sore of make sense if the above is the reason.
Here is the sample code:
And if you change it by enclosing the assignment in braces (creating a block) the error goes away:
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that putting a label on an variable declaration doesn't make sense anyway. The scope of the label is only the contained statement. So by the time the label could be used it would be out of scope.
 
Steven Squeers
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies.
I understand that braces make a block; what I can't fathom is how a System.out.println() qualifies as a block; perhaps though it doesn't need to be a block. Perhaps a method call and a block (formed with braces) are part of a larger set of syntax elements, and, an assignment is not part of that set. Hmm.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Squeers wrote:Thanks for the replies.
I understand that braces make a block; what I can't fathom is how a System.out.println() qualifies as a block;


System.out.println() is a method, not a block. A method call doesn't have to be immediately enclosed in a block. Nor does a label need to contain a block (it can contain a block, but that is because a block is a statement and a label statement contains a statement). A variable declaration does have to be immediately enclosed in a block.

perhaps though it doesn't need to be a block. Perhaps a method call and a block (formed with braces) are part of a larger set of syntax elements, and, an assignment is not part of that set. Hmm.


It isn't about what the label can do - it is the variable declaration that has special requirements.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Squeers wrote:Thanks for the replies.
I understand that braces make a block; what I can't fathom is how a System.out.println() qualifies as a block; perhaps though it doesn't need to be a block. Perhaps a method call and a block (formed with braces) are part of a larger set of syntax elements, and, an assignment is not part of that set. Hmm.



To follow the specification, System.out.println() is a MethodInvocation, which is a type of StatementExpression, which is part of ExpressionStatement (with the semicolon as the other part), which is a type of StatementWithoutTrailingSubstatement, which is a type of Statement, which is allowed to follow a label Indentifier (as defined by LabelStatement).

On the other hand, "int assignMe = 100" is a LocalVariableDeclaration, which is part of LocalVariableDeclarationStatement (with the semicolon as the other part), which is a BlockStatement, which is part of a BlockStatements (with other BlockStatement as the other parts). And that is it -- none of the items on this list is allowed to follow a label Indentifier (as defined by LabelStatement).

However, if curly braces were added then BlockStatements is allowed within it forming a Block, which is allowed to follow a label Indentifier (as defined by LabelStatement).


The Java Language Specification is as clear as mud right ?? ...

Henry
 
Steven Squeers
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's easier to just remember that an assignment can't follow a label, but a method invocation can.
Thanks!
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Squeers wrote:I think it's easier to just remember that an assignment can't follow a label, but a method invocation can.
Thanks!


But neither is real useful in a label statement, since there really isn't a chance to use the label for control transfer. Better is to use it with a loop, or a block which contains lots of statements.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Squeers wrote:I think it's easier to just remember that an assignment can't follow a label, but a method invocation can.



I don't think that's right. It's a declaration that can't follow a label; an assignment should be perfectly fine there. Try it out and see.
 
Steven Squeers
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Steven Squeers wrote:I think it's easier to just remember that an assignment can't follow a label, but a method invocation can.



I don't think that's right. It's a declaration that can't follow a label; an assignment should be perfectly fine there. Try it out and see.



Ah well spotted. So, an assignment is ok but a declaration is not. I can't see what the language designers have in mind here but it is what it is.

Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic