This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Declaration cannot be labelled Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Declaration cannot be labelled" Watch "Declaration cannot be labelled" New topic

Declaration cannot be labelled

Sridevi Shrikanth
Ranch Hand

Joined: Jan 11, 2001
Posts: 31
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++)

Could anyone explain to me the context in which it is invalid.
Any explanation is truly appreciated.
Thank you.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
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).
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
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.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Sridevi Shrikanth
Ranch Hand

Joined: Jan 11, 2001
Posts: 31
JDK 1.1.7. Probably thats why I get the error.
Thank you.
natarajan meghanathan
Ranch Hand

Joined: Feb 01, 2001
Posts: 130
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.

Sun Certified Programmer for Java 2 Platform
Velmurugan Periasamy
Ranch Hand

Joined: Nov 09, 2000
Posts: 95
I have the list of the potential SCJP traps in my study notes.
You can download it at

Velmurugan Periasamy
Sun Certified Java Programmer
Study notes for Sun Java Certification

------------------------------------------------------------------------------------<BR>Velmurugan Periasamy<BR>Sun Certified Java Programmer for Java 2 Platform<BR>Sun Certified Web Component Developer for J2EE platform<BR>Sybase Certified EAServer Developer (similar to jCert level 3)<BR>------------------------------------------------------------------------------------<BR>Study notes for Sun Java Certification<BR><A HREF="" TARGET=_blank rel="nofollow"></A><BR>------------------------------------------------------------------------------------
I agree. Here's the link:
subject: Declaration cannot be labelled
It's not a secret anymore!