• 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

Local variables in switch statement

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code:
public class sample
{
public static void main(String args[])
{
int i=2;
switch(i)
{
case 1:
int j=10;
break;
case 2:
//j=30; //line 1
System.out.println(j); //line 2
break;
case 3:
int k=40;
break;
}
}
}
The code doesn't compile and says that the variable might not have been initialized. But if i uncomment line 1 it compiles fine and prints the value j. Line 2 doen't declare the variable, it initializes it. This means that the compiler accepts the declaration of j in case 1 as valid in case 2. But why doesn't it accept the initialization of the variable? I am not quite able to understand this. Can anybody please explain to me?
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i tried modifying case 2:
case 2:
int j=2;//compilation error -variable already defined
j=30; //line 1
System.out.println(j);
now the compilation error proves that the variable j scope is the complete switch statement
but i still could not figure out a way to answer this question
Samith.P.Nambiar
 
Ranch Hand
Posts: 645
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi priya,
Before going to u r question fist make one thing clear
in u r mind in java there is no concept call garbage value
for variabels hence all variable r to be initilised
now in u question u r conceriding only one case i.e value of
i=2; in this case while execution control will go directly to
case 2 and will try to println value of j which is which is not
initilized. because it never went to case 1 to execute int j=10
so we r trying to print garbage value of j which is not posible
now. U Will Say But Why Cant i define int j=30 in case 2
this is because it has define at case 1 and compiler can detect
it while compiling casue it goes thow all case
..hope u get it
...praful
SCJP
 
Priya Chetlur
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In the code the control directly goes to the case 2. It doesn't go to case 1. In case 2 you are just assigning the value 30 to the already declared and initialized varible j. So how come the declaration of the int j in case 2 valid?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Priya,
I will try to answer your question.
- The scope for local variables is from the declaration statement to the end of the block. So if I had switch statement like
switch (i) {
case 1:
j = 30; // line 1
case 2:
int j = 10; // line 2
System.out.println(j); // line 3
}
This code will not compile because at line 1 variable j is not defined so scope is not for entire switch statement.
- in your code, if line 1 is commented out, there exists a path of execution that will use a local variable that has not been ASSIGNED. (System.out.prinln(j). Read this
From JLS quote:
-------
Definite Assignment
-------------------------------------------
Each local variable (�14.4) and every blank final (�4.5.4) field (�8.3.1.2) must have a definitely assigned value when any access of its value occurs. A Java compiler must carry out a specific conservative flow analysis to make sure that, for EVERY ACCESS of a local variable or blank final field f, f is definitely assigned before the access; otherwise a compile-time error must occur.
-------------------------------------------
Hope that helps
 
reply
    Bookmark Topic Watch Topic
  • New Topic