Help With switch-case

H

HugeBob

Hi All,

I'm studying for the SCJP 6 exam and I've run across an oddity.
Here's the code:

public class SwitchCheck {
public static void main(String [] args) {
final int a = 1, final int b = 2;

int x = 0;
switch (x) {
case a:
break;

case b:
break;
}
}
}

Why does this not work? Both a and b are final and receive their
value where they should. a and b are known at compile time. Yet I
get an error for the "case b:" statement.
 
E

Eric Sosman

HugeBob said:
Hi All,

I'm studying for the SCJP 6 exam and I've run across an oddity.
Here's the code:

public class SwitchCheck {
public static void main(String [] args) {
final int a = 1, final int b = 2;

int x = 0;
switch (x) {
case a:
break;

case b:
break;
}
}
}

Why does this not work? Both a and b are final and receive their
value where they should. a and b are known at compile time. Yet I
get an error for the "case b:" statement.

Your compiler appears to be broken; get a new one.
Mine gives no error on `case b:', having already bailed
out on an earlier line:

<identifier> expected
final int a = 1, final int b = 2;
^
1 error
BUILD FAILED (total time: 0 seconds)
 
R

Roedy Green

I'm studying for the SCJP 6 exam and I've run across an oddity.
Here's the code:

public class SwitchCheck {
public static void main(String [] args) {
final int a = 1, final int b = 2;

int x = 0;
switch (x) {
case a:
break;

case b:
break;
}
}
}

Why does this not work? Both a and b are final and receive their
value where they should. a and b are known at compile time. Yet I
get an error for the "case b:" statement.

You have a comma where you should have a semicolon. This is Java not
C++.

To sort out what you can use for case labels, see
http://mindprod.com/jgloss/switch.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"If everyone lived the way people do in Vancouver,
we would need three more entire planets to support us."
~ Guy Dauncey (born: 1948 age: 61)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top