This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes goto in java:error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "goto in java:error" Watch "goto in java:error" New topic
Author

goto in java:error

Preetham M
Greenhorn

Joined: Sep 18, 2002
Posts: 12
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

Joined: Dec 10, 2001
Posts: 7023
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!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
No goto in Java.
James Chegwidden
Author
Ranch Hand

Joined: Oct 06, 2002
Posts: 201
Yes, no goto. It is a reserved word but you can't use it. Same goes to const.


Author and Instructor, my book
Preetham M
Greenhorn

Joined: Sep 18, 2002
Posts: 12
So if i need to make a jump, its not possible in java..
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61084
    
  66

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Still thinking procedurally are we .


"JavaRanch, where the deer and the Certified play" - David O'Meara
James Chegwidden
Author
Ranch Hand

Joined: Oct 06, 2002
Posts: 201
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

Joined: Jun 06, 2002
Posts: 1056
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.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: goto in java:error