Results 1 to 14 of 14
Thread: Switch
- 03-20-2012, 10:31 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 55
- Rep Power
- 0
Switch
hi all,
i was a going through a book on switch statements which said " case constant must be a compile time constant that is it is assigned a literal value as soon as it is declared" And it gave the following code as an example saying that if the user attempts to run the code it will throw compile time error. below mentioned is the code
final int a=1;
final int b;
b=2;
int x=0
switch(x)
case a: ok
case b: compile time error
ok, now when i tried using the above the concept it doesn't throw me any error. FYI my java version is 5.0 and the book is also for java 5
Can anyone explain?
Thank you and regards!!
- 03-20-2012, 11:42 AM #2
Re: Switch
- 03-20-2012, 12:02 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 55
- Rep Power
- 0
Re: Switch
no i didn't
- 03-20-2012, 12:21 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 03-20-2012, 12:28 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 03-20-2012, 12:52 PM #6
Member
- Join Date
- Jan 2012
- Posts
- 55
- Rep Power
- 0
Re: Switch
grrr... the code that i mentioned above is just a sample code the book mentioned. am from my side i did some thing like this :
class Sws
{
public static void main(String[] ags)
{
final int x;
x=2;
switch(x)
{
case 1:
System.out.println("1");
break;
case 2:
System.out.println("2");
break;
default:
System.out.println("default");
}
}
}
Now this code using java 5 compiles fine. now the book named scjp by kathy sierra also for java 5 says that the above code will throw error unless am assigning the variable x in one single line.
what is the issue? was that a typing mistake by the book or what?
- 03-20-2012, 12:59 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Switch
As I suspected.
Can you spot the difference between your code here and that in your first post?
None of your case checks use a variable (whether final or not), whereas in your first post the case checks were against 'a' and 'b'.
Whether 'x' (the variable being checked in the switch) is a compile time constant or not is irrelevant.Please do not ask for code as refusal often offends.
- 03-20-2012, 01:08 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 03-20-2012, 01:14 PM #9
Member
- Join Date
- Jan 2012
- Posts
- 55
- Rep Power
- 0
Re: Switch
yes yes i knew it but i then the next question that comes is am i going through wrong book for scjp?
- 03-20-2012, 01:39 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Switch
I don't think so although it surprises me a bit that Kathy Sierra made such an obvious blooper; otoh, there are many Java books that only deserve to be in the fire place; people think they can make money just by putting 'Java' on the cover of their book while the inside just contains utter crap.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-20-2012, 02:00 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Switch
Can you post exactly the code in the Kathy Sierra book, because as I said, your code in post #1 is completely different to the code in post #6.
The original code highlights the problem described in the quoted text:
"
case constant must be a compile time constant that is it is assigned a literal value as soon as it is declared
"
Your code in post #6 does not.Please do not ask for code as refusal often offends.
- 03-20-2012, 06:35 PM #12
Member
- Join Date
- Jan 2012
- Posts
- 55
- Rep Power
- 0
- 03-20-2012, 06:37 PM #13
Member
- Join Date
- Jan 2012
- Posts
- 55
- Rep Power
- 0
- 03-21-2012, 09:53 AM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
switch statements
By jim01 in forum New To JavaReplies: 7Last Post: 04-10-2011, 10:52 PM -
switch
By dj kourampies in forum New To JavaReplies: 17Last Post: 01-30-2009, 05:32 PM -
switch
By dj kourampies in forum New To JavaReplies: 2Last Post: 01-30-2009, 08:46 AM -
Switch help please!!!!
By soc86 in forum New To JavaReplies: 6Last Post: 11-23-2008, 07:25 PM -
What is wrong with my switch??
By soc86 in forum New To JavaReplies: 3Last Post: 11-20-2008, 12:34 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks