Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Local Variable inside Switch

 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roopesh Gulecha
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typo : I meant doesn't even get declared...
 
anand phulwani
Ranch Hand
Posts: 242
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks sandeep. that was a good learning.
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And very well explained by Mr. Anand and Mr. Roopesh ......Thanx to them too...

Sandy
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx to all of you espacially to Anand to make my concepts more clear.
That was a nice point.
 
Patrick Punty
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic