• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can someone explain this statement in the JLS

 
Kourosh Keshavarzi
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is in section 4.5.3
Were it not for one exceptional situation, a local variable could always be regarded as being created when its local variable declaration statement is executed. The exceptional situation involves the switch statement, where it is possible for control to enter a block but bypass execution of a local variable declaration statement. Because of the restrictions imposed by the rules of definite assignment, however, the local variable declared by such a bypassed local variable declaration statement cannot be used before it has been definitely assigned a value by an assignment expression.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kourosh,
I think they're referring to a situation like the following:
<pre>
int i = 3;

switch( i ) {
case 0:
// do something
break;
case 1:
// do something
break;
case 2:

int j = 5; // code is never reached

break;
default:

// this won't work
// compile error: j may not have been initialized
// but if you add j=0 before the print, it will
// compile and run ok EVEN though the code to
// declare j is never executed

System.out.println(j);
}

</pre>
Even though the program flow shows that the statement int j =5; will never be executed; j is still treated as if it was created.
Hope that helps.
------------------
Jane
 
Kourosh Keshavarzi
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jane,
Your a freak'n genius
Thanks for your help
it was right on
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic