This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 60752
    
  65

"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]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: switch statement problem
 
Similar Threads
switch case with final
Switch Statement
final in switch case
Switch statement/case restrictions
Compile Time Constant