• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

label statement

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.......
^
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ^
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class testLabel
{
public static void main(String args[])
{
here: is: a: useless: father: who: gave: birth: to:;
}
}
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic