• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch statement problem

 
srini scjp
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Switch {

public static void main(String[] args) {

final int a = 1;
final int b;
b = 2;
int x = 0;

switch (x) {
case a: // ok
case b: // compiler error

}// end of switch statement

}// end of main method

}// end of class

Hi friends,

in the above problem, I am giving values to the final variables in main method. so, they won't get any default values.

why am i getting error for case b?

Thanks in Advance
srini
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look the arguments in the case can only be compile time contants..the error you get is because of violating that rule
 
srini scjp
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raju Champaklal wrote:look the arguments in the case can only be compile time contants..the error you get is because of violating that rule



Thank you very much for the immediate response. could you please elaborate the answer.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey refer the kathy siera book...there it is explained properly
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"srini scjp", please check your private messages for an important administrative matter.
 
Leonardo Carreira
Ranch Hand
Posts: 494
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if we want to declare a variable as final we've to initialize it too..
of course in that code, there's error while compile-time..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic