aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Labels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Labels" Watch "Labels" New topic
Author

Labels

K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
The following code works


but the follwoing doesn't


Please tell me the reason for this.
Victor Santos
Ranch Hand

Joined: Jan 12, 2004
Posts: 31
void crazyLoop() { JILL: int c = 0; System.out.println(c); JACK: while (c < 8) { if (c > 3) break JACK; else c++; } }

Hi !
in the second code , you are declaring int c at scope of label JILL, so it is visible only in this label . You can not mention variable c in Label JACK !
I hope this is the problem
K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
I don't think that's the problem. I thought labels work for loops only. But i first code Jill label is not used for loop then why does it work?
Chris Allen
Ranch Hand

Joined: Feb 01, 2003
Posts: 127
Labels can be placed on any Java statement, not just looping statements. The effect of placing them on non looping constructs is the limitation of scope they apply to. I believe Victor is correct in his scope comment as from the JLS (14.7):

Since the code is not enclosed in any braces, the scope is limited to the next statement ending with the semicolon. In this case, it really doesn't offer much in terms of functionality but it is legal.
K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
Originally posted by victor santos:
void crazyLoop() { JILL: int c = 0; System.out.println(c); JACK: while (c < 8) { if (c > 3) break JACK; else c++; } }

Hi !
in the second code , you are declaring int c at scope of label JILL, so it is visible only in this label . You can not mention variable c in Label JACK !
I hope this is the problem


DOes that mean this should work


Now i am not using c anywhere ut still it doesn't work.
Pal Sudarshan
Ranch Hand

Joined: Jun 10, 2004
Posts: 52
Hello,

I am not sure of the answer. Maybe I shouldn't be posting this, but here is what I think as of now. Maybe it will of help, but I AM NOT SURE of my own answer, okay. Just to let you know. :roll:

I think JILL: int c = 0 is not a statement.
Here is what I found online about statements in Java.
Click on the link below.
http://www.sniffer.net/bookshelf_do_sniffer/java/langref/ch06_js.htm
vydhehi paruchuri
Greenhorn

Joined: Aug 27, 2003
Posts: 22
According to JLS these are the statements:

Statement:
StatementWithoutTrailingSubstatement
LabeledStatement
IfThenStatement
IfThenElseStatement
WhileStatement
ForStatement

StatementWithoutTrailingSubstatement:
Block
EmptyStatement
ExpressionStatement
SwitchStatement
DoStatement
BreakStatement
ContinueStatement
ReturnStatement
SynchronizedStatement
ThrowStatement
TryStatement

StatementNoShortIf:
StatementWithoutTrailingSubstatement
LabeledStatementNoShortIf
IfThenElseStatementNoShortIf
WhileStatementNoShortIf
ForStatementNoShortIf

I think, Assignment statement is not pure statement.

You can compile and run the program by changing the label to

JILL:
{
int p = 0;
}
Thanks,

Vydhehi
siva prakash
Ranch Hand

Joined: Aug 24, 2004
Posts: 80
Hi ,

void func() {

JACK : {
int i = 10 ;
}
}


works , but not

void func() {

JACK : int i = 10 ;
}


bye
siva
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Labels