aspose file tools*
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes Labeled statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "Labeled statements" Watch "Labeled statements" New topic
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: 251
    
    9
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 Manning's OCA Java SE 7 Programmer I Certification Guide and OCP Java SE 7 Programmer II Certification Guide
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: 251
    
    9
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: 251
    
    9
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: 251
    
    9
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: 22
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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Labeled statements