Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

goto in java:error

 
Preetham M
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all:
I have the code:
class A{
.
.
public void asd()
{
StartEx:
int i;
.
.
goto StartEx;
}
}
When i compile the code i get the error..
'.class' expected
int i;
^
illegal start of expression
goto StartEx;
^
Could somebody help me with why the error occurs.
Thanks,
Preetham.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preetham M,
Welcome to JavaRanch (again)!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No goto in Java.
 
James Chegwidden
Author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, no goto. It is a reserved word but you can't use it. Same goes to const.
 
Preetham M
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if i need to make a jump, its not possible in java..
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Preetham M:
So if i need to make a jump, its not possible in java..

Well, trust me - you don't *need* to!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Listen to Ilja's wise advice. I haven't used a goto since I stopped using BASIC in 1978. If you feel that you need to use a goto, it's probably a sign that you need to take a step back and take a look at the structure of your code.
hth,
bear
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still thinking procedurally are we .
 
James Chegwidden
Author
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with the above. You do not need to make jumps. If you want to "jump" you can use a break or continue with a label. This can "jump" in your code.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even in C or C++ you should normally use goto only to exit loops. In Java you do not need goto for this purpose because you can use break labels instead.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic