|
-
Mako Shark
Stupid Java question, why isnt switch working outside of the main method?
ok, after i cut out all of the inside stuff heres where im getting the error
Code:
class Switchtest {
switch(5) {
default:
System.out.println("p");
}
}
That should be valid right? Well heres the error im getting
Code:
d:\prog\java>javac switch.java
switch.java:4: illegal start of type
switch(5) {
^
switch.java:7: <identifier> expected
}
^
2 errors
d:\prog\java>
The second error is due to the first one, since its not recongizing switch, though WHY? When i add in the main method it compiles just fine.
Code:
class Switchtest {
public static void main(String[] args) {
switch(5) {
default:
System.out.println("p");
}
}
}
I should be using the 1.4.2 Java SDK
-
You class file doesn't have a constructor or method or anything. You class files have to have some sort of method.
-
Hammerhead Shark
Originally posted by anfpunk
You class files have to have some sort of method.
No, they don't.
is valid.
However, the point I think you're trying to make is correct... the switch statement must be in a constructor, method or static initializer.
Stay cool
and be somebody's fool this year
-
You are very correct. That is a common way of satisfying classes and methods that haven't been written yet. I need to be more careful is how I say things.
-
Mako Shark
thanks 
<--- the dangers of taking first java class last fall, not practicing it for a year, and expecting to be perfect.
Last edited by Mancora; 09-22-2003 at 12:26 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|