Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch statement

 
kish kumar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
b isnt a compile time constant as it is not initilaised at the same line where it was declared
 
Alpesh Rathod
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot have final variables as case constants .......
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
kish kumar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup
 
kish kumar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic