The moose likes Associate Certification (SCJA/OCAJ) and the fly likes Labeled statements Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Associate Certification (SCJA/OCAJ)
Reply 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
Ranch Hand

Joined: Sep 27, 2002
Posts: 126
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


SCWCD, SCJP
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
Ranch Hand

Joined: Sep 27, 2002
Posts: 126
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
Ranch Hand

Joined: Sep 27, 2002
Posts: 126
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
Ranch Hand

Joined: Sep 27, 2002
Posts: 126
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;
}
}
}
 
IntelliJ Java IDE
 
subject: Labeled statements
 
Threads others viewed
Anonymous array
create calendar java
program that prints the multiplication table up to one
Strign of arrays
Why is this printing 1,0 instead of 1,1
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture