If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > JAVA > CASE statetment with Global variables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-21-10, 07:07
fshuk1 fshuk1 is offline
Registered User
 
Join Date: Aug 2010
Posts: 3
Question 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
Reply With Quote
  #2 (permalink)  
Old 08-22-10, 10:36
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
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.
__________________
Mike
Reply With Quote
  #3 (permalink)  
Old 08-22-10, 11:39
fshuk1 fshuk1 is offline
Registered User
 
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?
Reply With Quote
  #4 (permalink)  
Old 08-22-10, 12:29
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
You need to use final which means the value cannot be changed. The link explains a little about the difference between static and final.
__________________
Mike
Reply With Quote
  #5 (permalink)  
Old 08-22-10, 16:21
fshuk1 fshuk1 is offline
Registered User
 
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 ?????
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On