aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes switch statement problem 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 problem" Watch "switch statement problem" New topic
Author

switch statement problem

srini scjp
Greenhorn

Joined: Jan 01, 2010
Posts: 2
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

Joined: Dec 10, 2009
Posts: 521
look the arguments in the case can only be compile time contants..the error you get is because of violating that rule


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
srini scjp
Greenhorn

Joined: Jan 01, 2010
Posts: 2
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

Joined: Dec 10, 2009
Posts: 521
hey refer the kathy siera book...there it is explained properly
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61180
    
  66

"srini scjp", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489


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..


Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: switch statement problem