File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

label statement

 
mridul makkar
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Labels must immediately follow a block, and a block is not necessarily a for-loop:

 
mridul makkar
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class testLabel
{
public static void main(String args[])
{
here: is: a: useless: father: who: gave: birth: to:;
}
}
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic