Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2010
    Posts
    3

    Question Unanswered: CASE statetment with Global variables

    Hi guys,
    I am a C programmer trying to write in JAVA !!!! (so new to JAVA)
    i have declared some public static int variables in my Main class which I want to use in another classes. In one class i have a switch statement
    switch (tickerId){
    case Main.ftse_contract_id: ftse_data.writeToFile(msg);break;
    case Main.ftsefut_contract_id: ftsefut_data.writeToFile(msg);break;
    case Main.indu_contract_id: indu_data.writeToFile(msg);break;
    case Main.lloy_contract_id:: lloy_data.writeToFile(msg);break;
    case Main.barc_contract_id: barc_data.writeToFile(msg);break;
    case Main.vix_contract_id: vix_data.writeToFile(msg);break;
    }
    this is giving me the usual "constant expression required" error. The problem is with Main.xxx_contract_id ints that I declared before to be used everywhere. Can someone please tell me either a better way of having GLOBAL ints or how to make the CASE statements work.

    Thank you

  2. #2
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    I believe the values following the case statement has to be a constant and not a variable. If those values can't be expressed as constants then just replace the switch statement with if then elses.

  3. #3
    Join Date
    Aug 2010
    Posts
    3

    agree

    I agree with you mike. That is why i have declared my variables as public static int. Doesn't the static part do this? is there anotherway i should declare them?

  4. #4
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    You need to use final which means the value cannot be changed. The link explains a little about the difference between static and final.

  5. #5
    Join Date
    Aug 2010
    Posts
    3

    Red face changing static to final

    hi Mike
    changing my variable from public static int to public final int unfortunately just changed the error message from above to this


    non-static variable ftse_contract_id cannot be referenced from a static context
    --

    any ideas ?????

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •