• 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 Problem

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This code compiles.
But if the control goes to case 'b' directly,
how does JVM knows if int j is declared? can somebody please explain?


switch(digit)
{
case 'a': int j=10; break;
case 'b': j=1;
System.out.println(j);
}

------------------
switch(digit)
{
case 'a': int j=10; break;
case 'b': System.out.println(j);
}
this is compile time error -
variable j might not have been initialized
----------------------
switch(digit)
{
case 'a': int j=10; break;
case 'b': int j; System.out.println(j);
}

this is compile time error -
duplicate definition of variable j in method main(java.lang.String[]) compiletime error
----------------------------
switch(digit)
{
case 'a': j=1; System.out.println(j);
case 'b': int j=10; break;
}

compile time error
variable j not found in class mypackage.Example1
[ January 05, 2006: Message edited by: V Jagannadh ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must declare the variable int j outsite the switch case curly braces.
like this:
int j;
switch(digit){
case a:
j = 8;
break;
case b:
j = 9;
break;
}

because when you declare the variable in the case
scope of the variable is within the case.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by V Jagannadh:
Hi,

This code compiles.
But if the control goes to case 'b' directly,
how does JVM knows if int j is declared? can somebody please explain?


switch(digit)
{
case 'a': int j=10; break;
case 'b': j=1;
System.out.println(j);
}
[B] The scope of variable is code block in which it is declared. curly braces '{' and '}' detremine boundaries of a code block. So in the above code variable j is visible to both cases.
modify the code in the following way, by adding a new code block and see code will not compile


[/b]
------------------
switch(digit)
{
case 'a': int j=10; break;
case 'b': System.out.println(j);
}
this is compile time error -
variable j might not have been initialized
This is because in the case 'b' variable can be accessed without intialisation. This is java safe gaurd against accessing any un intialised variable

----------------------
switch(digit)
{
case 'a': int j=10; break;
case 'b': int j; System.out.println(j);
}

this is compile time error -
duplicate definition of variable j in method main(java.lang.String[]) compiletime error
this shall follow from explanation of first.
----------------------------
switch(digit)
{
case 'a': j=1; System.out.println(j);
case 'b': int j=10; break;
}

compile time error
variable j not found in class mypackage.Example1

No forward declrations in Java

[ January 05, 2006: Message edited by: V Jagannadh ]



please see inline comments in bold
regards
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I'd like to know this too.
Does anybody understand why it compiles and run ?
 
vivekkumar sharma
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Satou kurinosuke:


I'd like to know this too.
Does anybody understand why it compiles and run ?



Hi Satou ,
whats the difference in it from the origional question ?
My applogies if the previous reply does not appear well formated
regards
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivek,

Thanks for detailed explanation. i learnt something new..But i have some questions.



Here second case 'b' will be selected & executed, but we are declaring 'j' in first case 'a' which will not be executed. So from where second case it is taking j's declaration?

Second question:



why it is giving "variable j might not have been initialized"? As it is getting intialized at the same place where it is declared. So second case should take either both declaration & intilazation OR none.

Thanks
Gagan.
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
vivekkumar sharma,
If your explanation of curly braces is true, the following should compile



When we put '{', the boundaries of the code-block are explicitly set.
For conditional statements if the boundaries are not specified explicitly, it will follow the 'rules for implicitly set'.

There is a difference between implicitly and explicitly specifying the boundaries of a code block. For if statement the boundaries seems to be same for both implicit and explicit specification.

For Switch statement it seems to be different. I am not aware why it is so. But I am sure this is the direction we have to go.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic