• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Declaration cannot be labelled

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JDK 1.1.7. Probably thats why I get the error.
Thank you.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic