Help With switch-case

Discussion in 'Java' started by HugeBob, Feb 26, 2009.

  1. HugeBob

    HugeBob Guest

    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.
     
    HugeBob, Feb 26, 2009
    #1
    1. Advertising

  2. HugeBob

    Eric Sosman Guest

    HugeBob wrote:
    > 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)

    --
    Eric Sosman
    lid
     
    Eric Sosman, Feb 26, 2009
    #2
    1. Advertising

  3. HugeBob

    Roedy Green Guest

    On Thu, 26 Feb 2009 04:35:49 -0800 (PST), HugeBob <>
    wrote, quoted or indirectly quoted someone who said :

    >
    >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)
     
    Roedy Green, Feb 26, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gane kol

    switch case with enum

    gane kol, May 6, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    7,328
    Kevin Spencer
    May 6, 2005
  2. Brian J. Sayatovic

    Why constant ints in switch case expressions?

    Brian J. Sayatovic, Jul 2, 2003, in forum: Java
    Replies:
    22
    Views:
    21,912
    Dale King
    Jul 9, 2003
  3. vic

    switch case statement

    vic, Mar 3, 2004, in forum: Java
    Replies:
    9
    Views:
    5,989
  4. Myster Ious
    Replies:
    10
    Views:
    1,139
    Myster Ious
    Aug 1, 2003
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    123
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page