Results 1 to 4 of 4
  1. #1
    jarek is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default switch() don't work with constant

    Java Code:
    class Class_A {
        public static final int TT_EOL = 10;
    }
    
    class TestSwitch {
        public static void main(String args[]) {
            Class_A obj = new Class_A();
    
            int i = 10;
            switch (i) {
                case Class_A.TT_EOL : //but expression 'obj.TT_EOL' don't work
                    System.out.println("i = " + i);
                    break;
            }
        }
    }
    any ideas?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You need to specify a compile time constant expression. (See JLS 15.28). TypeName.Identifier is OK if it refers to a constant variable, but the compiler is not going to go investigating obj to see that it's a constant 10.

    -----------

    obj.TT_EOL is wrong anyway ;)

  3. #3
    jarek is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default *

    Thank a lot! :)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

Similar Threads

  1. static constant and performance
    By felixtfelix in forum New To Java
    Replies: 2
    Last Post: 08-31-2010, 02:43 PM
  2. Replies: 2
    Last Post: 06-08-2010, 02:21 PM
  3. switch
    By dj kourampies in forum New To Java
    Replies: 17
    Last Post: 01-30-2009, 06:32 PM
  4. switch
    By dj kourampies in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 09:46 AM
  5. Constant size for JTextArea
    By itaipee in forum AWT / Swing
    Replies: 1
    Last Post: 12-30-2008, 05:12 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
  •