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

Declaration cannot be labelled

 
Sridevi Shrikanth
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In the potential traps as given by Ajith, one of the traps says
Declaration cannot be labelled.

However the following code runs perfectly well.
public class Decl
{

private static void main(String[] args)
{
label1: float f = 1.3f;
label2 : for(int i=0;i<10;i++)
{
System.out.println(i);
}

}
}
Could anyone explain to me the context in which it is invalid.
Any explanation is truly appreciated.
Thank you.
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you can label anything in your program, but you can't use a break or continue with a label unless what immediatly follows the label is a loop for continue and break or a switch statement for break only (not continue).
Bill
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sridevi, which compiler are you using? I cannot compile your program ( Suns' javac ( JDK1.3 ) compiler ). I get an error, as expected, that says " A declaration cannot be labeled" indicating the label1: float f = 1.3f ; statement.

------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
 
Sridevi Shrikanth
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JDK 1.1.7. Probably thats why I get the error.
Thank you.
 
natarajan meghanathan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sirdevi,
I think declarations cannot be labeled.
Btw, can anyone tell me where the traps as listed by ajith or somebody else is in the web.
TIA.
 
Velmurugan Periasamy
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the list of the potential SCJP traps in my study notes.
You can download it at http://www.geocities.com/velmurugan_p/

------------------
Velmurugan Periasamy
Sun Certified Java Programmer
----------------------
Study notes for Sun Java Certification
http://www.geocities.com/velmurugan_p/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic