| Author |
Labeled statements
|
Fahad Muhammad
Greenhorn
Joined: Nov 10, 2010
Posts: 6
|
|
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
Joined: Sep 27, 2002
Posts: 196
|
|
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
|
Author of book OCA Java SE 7 Programmer I Certification Guide from Manning
|
 |
Fahad Muhammad
Greenhorn
Joined: Nov 10, 2010
Posts: 6
|
|
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
Joined: Sep 27, 2002
Posts: 196
|
|
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
Joined: Nov 10, 2010
Posts: 6
|
|
I mean this which will not compile
Versus this wich will compile
|
 |
Mala Gupta
Author
Ranch Hand
Joined: Sep 27, 2002
Posts: 196
|
|
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
Joined: Sep 27, 2002
Posts: 196
|
|
A little funny image may help as well:
cheers
Mala
|
 |
Fahad Muhammad
Greenhorn
Joined: Nov 10, 2010
Posts: 6
|
|
|
Thank you very much Mala
|
 |
rohit sahai
Greenhorn
Joined: May 11, 2012
Posts: 1
|
|
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
Joined: Jul 17, 2012
Posts: 20
|
|
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:
|
 |
 |
|
|
subject: Labeled statements
|
|
|