• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Switch case

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic