Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Labeled statements

 
Fahad Muhammad
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

why this code doesn't compile



this will compile



also this will compile



The difference I can see is using blocks { } and for block.
Are there any rules regarding labeling statements?

Thanks in advance.
 
Mala Gupta
Author
Ranch Hand
Posts: 291
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the name implies, a labeled statement is supposed to define a set of statements, which can be referred using a label. A set of statements can be defined as a block by either grouping all the statements using {} or by using any of the loops - while/ do-while/ for. When you issue the command break label or continue label, JVM should know the block of statements it is supposed to exit/ start over again.

cheers
Mala
 
Fahad Muhammad
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mala.

I also noticed that declaring variables inside a labeled statement causes a compilation error.
So, Is it safe to say that declaring variable is NOT a statement?
 
Mala Gupta
Author
Ranch Hand
Posts: 291
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fahad Muhammad wrote:
I also noticed that declaring variables inside a labeled statement causes a compilation error.
So, Is it safe to say that declaring variable is NOT a statement?


Fahad,

I modified your code and added a line of variable declaration within the labeled statement and it compiles fine:



Or did you mean something else?

cheers
Mala
 
Fahad Muhammad
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean this which will not compile


Versus this wich will compile

 
Mala Gupta
Author
Ranch Hand
Posts: 291
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fahad,

Labeled statements do not work with declarations. The following is 'labeled' declaration, which won't work:


Labels can be used with a looping statements (for, while, do), if constructs (if statement), expression, assignment, return, try, throws, and with a block. It is interesting to note that the above declaration can be defined within a block statement as follows:


A little, funny & weird story may help you to remember this Java Rule:

Boss (Java) was announcing bonus (privilege of using labels) for all its employees (language constructs) that were present in the office. 'declaration' wasn't awarded this privilege because it was away, partying, and hence missed on it.

I know its very weird and funny, but it will help you to retain this info!

cheers
Mala
 
Mala Gupta
Author
Ranch Hand
Posts: 291
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A little funny image may help as well:



cheers
Mala
 
Fahad Muhammad
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Mala
 
rohit sahai
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mala

can you please tell me why the class does not comile ,if assignment is allowed


public class Breaker {
static String o = "";
public static void main(String[] args) {
z:
o = o + 2;
for(int x = 3; x < 8; x++) {
if(x==4) break;
if(x==6) break z;
o = o + x;
}
}
}
 
Cyril Sadasivan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rohit sahai wrote:Hi Mala

can you please tell me why the class does not comile ,if assignment is allowed


public class Breaker {
static String o = "";
public static void main(String[] args) {
z:
o = o + 2;
for(int x = 3; x < 8; x++) {
if(x==4) break;
if(x==6) break z;
o = o + x;
}
}
}

Rohit, the problem is not with the assignment statement. The label 'z' is applicable only for the statement "o=o+2;"

is same as

The "break z;" statement is within the for loop which is an unlabeled block.
For labeled break statements to work, the "break label" should appear within a block labeled with the particular label.
The code should be rewritten as follows:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic