This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can someone explain this statement in the JLS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can someone explain this statement in the JLS" Watch "Can someone explain this statement in the JLS" New topic
Author

Can someone explain this statement in the JLS

Kourosh Keshavarzi
Ranch Hand

Joined: Oct 23, 2000
Posts: 66
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

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Kourosh Keshavarzi
Ranch Hand

Joined: Oct 23, 2000
Posts: 66
Jane,
Your a freak'n genius
Thanks for your help
it was right on
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can someone explain this statement in the JLS
 
Similar Threads
local variable/switch
Question on if and object creation
final variable in switch
duplicate error
Local Variable inside Switch