• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch case

 
Aabha Varma
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic