wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Switch statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Switch statement" Watch "Switch statement" New topic
Author

Switch statement

kish kumar
Ranch Hand

Joined: Jan 03, 2008
Posts: 54



this code gave compile time error. The rule says a and b must be compile time constants. a and b are assigned values 1 ,2 at compile time only.so what is the issue?
can any one explain
champak bhumia
Greenhorn

Joined: Jan 11, 2010
Posts: 13
b isnt a compile time constant as it is not initilaised at the same line where it was declared


living with such a name wasn't easy for me
Alpesh Rathod
Ranch Hand

Joined: Jan 06, 2009
Posts: 41
You cannot have final variables as case constants .......


Thanks,
Alps
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
Alpesh Rathod wrote:You cannot have final variables as case constants .......

Final variables are constant their value does not change, Problem is switch accepts only compile time constants.
Final variables declared and initialised on one line are compile time constants like 'a' in the example so switch will accept it.
but final variables declared on one line and initialised on some other line are not compile time constants like 'b' in the code so switch will not allow it.

topic has been discussed earlier like here or here


SCJP 1.6 96%
kish kumar
Ranch Hand

Joined: Jan 03, 2008
Posts: 54

The gist is
"final variables are assigned values at compile time.
If final variable declared at one line and assigned value at next line, the value to final variable will be assigned at run time.
non final variables are assigned values at run time."

Correct Neha !

Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
yup
kish kumar
Ranch Hand

Joined: Jan 03, 2008
Posts: 54

There is a small correction :

if ther is a reference final Integer k = 10;

even though we are initializing it at the same line it wont be a compile time constant.If i say case k in a switch statement itwill give compiler error.
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

Yes you are correct, because Wrappers are also initialized at run time even if you have provided the value while declaring.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Harpreet janda wrote:Wrappers are also initialized at run time even if you have provided the value while declaring.



Exactly, for more clarification

is converted to

by the Compiler
Ann Johnston
Greenhorn

Joined: Feb 10, 2010
Posts: 1
So... Can I use an Integer in the switch bit, but not in the case bits?

Integer x = new Integer(3);
switch (x) {
case 1: System.out.println("value is 1"); break;
case 2: System.out.println("value is 2"); break;
case 3: System.out.println("value is 3"); break;
default: System.out.println("don't know value");break;
}

Annie0
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Ann Johnston wrote:So... Can I use an Integer in the switch bit, but not in the case bits?

Integer x = new Integer(3);
switch (x) {
case 1: System.out.println("value is 1"); break;
case 2: System.out.println("value is 2"); break;
case 3: System.out.println("value is 3"); break;
default: System.out.println("don't know value");break;
}


For switch bit, that's not a problem. We can use....


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Ann Johnston wrote:So... Can I use an Integer in the switch bit, but not in the case bits?

Wouldn't you get an answer if you try to compile the program that you just created??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
GeeCON Prague 2014
 
subject: Switch statement