• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Switch statement

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b isnt a compile time constant as it is not initilaised at the same line where it was declared
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot have final variables as case constants .......
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup
 
kish kumar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic