aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes label statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "label statement " Watch "label statement " New topic
Author

label statement

mridul makkar
Greenhorn

Joined: Feb 15, 2005
Posts: 14
if i put a lable in the decalration statement so it gives error , whats the reason behind that....

class mridul
{
public static void main(String args[])
{
m2: int i=1;
System.out.println("mridul makkar");
}
}

this gives error like
test1.java:6: '.class' expected
m2: int i=1;
^
test1.java:6: not a statement
m2: int i=1;

thanks in advance.......
^
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
According to JLS this is not a proper java statement because Label statement should come before loops only.

lab:
for(int i=0;i<n;i++)
break R continue lab;
it works because label followed by for,while,do while

lab:
System.out.println("Something");
for(int i=0;i<n;i++)
break R continue lab;
it doesn't works because label is followed by different statement
mridul makkar
Greenhorn

Joined: Feb 15, 2005
Posts: 14
wrong ans , u can put label anywhere in the programe .thats i checked , also the code u sent is also working.

class test
{
public static void main(String args[])
{
mridul: System.out.println("mridul makkar");
int i=10;
}
}
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
no use of using label, as you use label
one question for you then how you use label and return to that place???

This is my detail pgm

class test
{
public static void main(String args[])
{
mridul: System.out.println("mridul makkar");
for(int i=0;i<3;i++)
{
continue mridul;
}}
}

Error message shown by compiler

test.java:8: undefined label: mridul
continue mridul;


M.VIDYASAGAR ^
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Labels must immediately follow a block, and a block is not necessarily a for-loop:

mridul makkar
Greenhorn

Joined: Feb 15, 2005
Posts: 14
Vidya , just see ur code........

class test
{
public static void main(String args[])
{
mridul: System.out.println("mridul makkar");
for(int i=0;i<3;i++)
{
continue mridul;
}}
}
continute wants a block with a label named mridul,also it should be associated to for loop too,which it is not getting thats why it is giving this error.


Alton , i got it label should be in front of some block, so why it fails when i am puting in front of a declaration .i could say it is a single statement block,is not it??
also the error message is saying :-- '.class' expected
what it means.....
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by mridul makkar:
Alton , i got it label should be in front of some block, so why it fails when i am puting in front of a declaration


Your understanding of a block is incorrect.

This is not a block:


This is a block:
mridul makkar
Greenhorn

Joined: Feb 15, 2005
Posts: 14
Vidya , just see ur code........

class test
{
public static void main(String args[])
{
mridul: System.out.println("mridul makkar");
for(int i=0;i<3;i++)
{
continue mridul;
}}
}
continute wants a block with a label named mridul,also it should be associated to for loop too,which it is not getting thats why it is giving this error.


Alton , i got it label should be in front of some block, so why it fails when i am puting in front of a declaration .i could say it is a single statement block,is not it??
also the error message is saying :-- '.class' expected
what it means.....
Aruna Agrawal
Ranch Hand

Joined: Jan 27, 2005
Posts: 66
public class testLabel
{
public static void main(String args[])
{
here: is: a: useless: father: who: gave: birth: to:;
}
}
 
Consider Paul's rocket mass heater.
 
subject: label statement