wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Local Variable inside Switch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Local Variable inside Switch" Watch "Local Variable inside Switch" New topic
Author

Local Variable inside Switch

Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi,
please Consider the following code:


The following code raises the following compiler error:
-------------------------------------------------------
test40.java:11: variable x might not have been initialized
System.out.println(x);
^
1 error
-------------------------------------------------------
I wnt to know that the compiler does not complains about the existance of the variable x (cannot resolve symbol x) but in complains that it may be uninitilized.
when the compiler can see that the variable has been declared inside the scope of switch, then why cant it see that it has also been initilized?

i hope its not a silly question.

Thanx
Sandy


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Arvind Giri
Ranch Hand

Joined: Jun 26, 2005
Posts: 91
hi sandeep,
just think what will happen if case 1 is not executed.
in that case variable x have not been initialized.

hope that helps you


Regards<br /> <br />Arvind Giri<br />MCA,SCJP 1.4,SCWCD 1.4<br />Looking for SCDJWS
Roopesh Gulecha
Ranch Hand

Joined: Aug 09, 2005
Posts: 32
Arvind,
But the point here is that if Case 1 is not executed, then the variabel x doesn't even get executed. But the compiler points to the variable not being initialized rather than that the variable does not exist. So Sandeep may have a point and there may be more to this OR else the error message may be intended for both initialization and declaration. Not sure??
But ofcourse if the declaration is made final, it flags no error.


Roopesh.
Roopesh Gulecha
Ranch Hand

Joined: Aug 09, 2005
Posts: 32
Typo : I meant doesn't even get declared...
anand phulwani
Ranch Hand

Joined: Sep 10, 2005
Posts: 242
I Would really appreciate Mr Sandeep to bring that point in front of us,
this was really nice to solve,although this is another "Compiler Not Too Smart" thing with us,ok what i have got from this



is that
1)The case statement does not treat the statements in a separate block,rather these case statements act as a labels;
2)Whatever is declared in case: labels is executed for sure,till it finds a valid label for the switch.(mind it i call it a Declaration not intialisation )
3)Initialisation takes place only in valid case: when it runs;
4)But our "not to smart compiler" knows that you are trying to access the variable,which is initialized in another case:,which might not be executed,so it gives the error
----------------------------------------------------------------------
Test.java:11: variable x might not have been initialized
System.out.println(x);
^
1 error
-----------------------------------------------------------------------

Now To Further reolve the issue
using the code


gives the required error at compile time
--------------------------------------------------------------------
Test.java:13: cannot find symbol
symbol : variable x
location: class Test
System.out.println(x);
^
1 error
--------------------------------------------------------------------
and this runs finally to prove my conclusion



which compile fine and gives the output
---------------------------------------------------------------
29
---------------------------------------------------------------


Again Thanking Mr Sandeep To Bring Such A
Beautiful Point To Us.
With Lots Of Luck,
Anand


Thanks and Regards, Anand
SCJP 5.0 310-055 73%, SCWCD 1.4 310-081 78%, IBM DB2 9 Fundamentals 000-730 62%
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
thanks sandeep. that was a good learning.


/** Code speaks louder than words */
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
And very well explained by Mr. Anand and Mr. Roopesh ......Thanx to them too...

Sandy
Arvind Giri
Ranch Hand

Joined: Jun 26, 2005
Posts: 91
thanx to all of you espacially to Anand to make my concepts more clear.
That was a nice point.
Patrick Punty
Greenhorn

Joined: Jul 01, 2005
Posts: 24
As was reading your querry .........
I must say it was really a very good information thanks a lot to all of you.
Thomas Drew
Ranch Hand

Joined: Sep 15, 2004
Posts: 47
I don't know if this throws a Monkey Wrench into to the works but I typed in the code listed below and it ran. So my question is since the varible x is created in case 1. How i'm i able to assign a value to it in case 2 since case 1 is never called ?

anand phulwani
Ranch Hand

Joined: Sep 10, 2005
Posts: 242
i have given detailed explaination to this above,do read the posts before posting any question,i am sure you are going to find awnser it that.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
From the Java Language Specification (3rd Edition, 4.12.3 Kinds of Variables):

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 (�14.11), 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 (�16), 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 (�15.26).


Perhaps the above helps you understand the behaviour of local variable in a switch block. (I used the HTML version, and the FireFox browser to search the index for "switch", it immediately came up with the above information).

A local variable in a switch block must be defined "lexically" before it is used in the block. Program flow is not taken into account to check if the variable has been defined. But program flow is taken into account to ensure that a variable has been assigned a value before it is used in an expression.
[ September 18, 2005: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Local Variable inside Switch
 
Similar Threads
About switch...
implicit casting of final variables
Question about local variables in switch
Complie time Constant - final int a; a =1 ; IS NOT?!
why doesnt this compile?