aspose file tools*
The moose likes Beginning Java and the fly likes Switch case Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Switch case" Watch "Switch case" New topic
Author

Switch case

Aabha Varma
Greenhorn

Joined: Jan 12, 2005
Posts: 22
Hi

I am playing around with switch statement. I have a big doubt.
check these two code snippets
int j = 10
switch(j){
case 1 : int k = 5;methodA(k);break;
case 2 : int k = 10; methodA(k);break;
}
this gives a compilation error saying variable defined already
but the one below doesnt ..
int j = 10
switch(j){
case 1 : {int k = 5;methodA(k);break};
case 2 : {int k = 10; methodA(k);break};
}
why is it so ?

Second thing
int j = 10
switch(j){
case 1 : int k = 5;methodA(k);break;
case 2 : k=10;methodA(k);break;
}
Its defined already in case1, now it gives compilation errror with initialization....but in case 1 its already initialized.

Any one out there to help me ??
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

Because the integer k is defined twice in the same block.

Note that if you remove the "break;" at the end of a case, the code below the case continues to execute. The code of the case below is in the same block of code, if you don't surround it by "{" and "}".

You start a new block by using an opening "{". If you declare the variable k inside the block, it will only exist until the closing "}" of the block.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Here is a slightly better way to do the same thing.

int k;


if you are calling the same method for each and ever case, why not put it right after the switch block?


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

a variable goes out of scope when you hit the closing brace of the block in which is was declared. so, in your first example, k does not fall out of scope at the end of case 1. you then try and re-declare it in case 2, hence the error.

in your example 2, you put braces around the code for each case. therefore, when you leave a case, that variable is out of scope, and thus gone. you are therefore able to re-declare it.

in your third example, it is possible to get to the "k=10" without hitting the "int k=5", if j happens to equal 2 when the code runs. so, the compliler is complaining that it is POSSIBLE that k is never initialized.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Originally posted by fred rosenberger:

in your third example, it is possible to get to the "k=10" without hitting the "int k=5", if j happens to equal 2 when the code runs. so, the compliler is complaining that it is POSSIBLE that k is never initialized.


Value of k is used in methodA(k), but the assigment k=10 initializes the variable k, so the compiler must not complain about uinitialized variable.

Only error is missing ; after int j = 10 .
[ August 11, 2006: Message edited by: Vlado Zajac ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Switch case