aspose file tools*
The moose likes Java in General and the fly likes labels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "labels" Watch "labels" New topic
Author

labels

Steven Squeers
Ranch Hand

Joined: Aug 19, 2013
Posts: 62
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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

It's kind of hard to say without seeing the rest of the code, and what the EXACT compilation error is.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1939
    
  28
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

Joined: Aug 19, 2013
Posts: 62
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3947
    
  17

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
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3947
    
  17

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

Joined: Aug 19, 2013
Posts: 62
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

Joined: Jan 28, 2003
Posts: 3947
    
  17

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
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

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

Joined: Aug 19, 2013
Posts: 62
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

Joined: Jan 28, 2003
Posts: 3947
    
  17

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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

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

Joined: Aug 19, 2013
Posts: 62
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: labels
 
Similar Threads
Why I have this error?
Prefrence to local ?
Local variable scope
Explain this generics code?
data types