The below code throws the compiler error: "Local variable may not have been initialized" on Line 6.
However the code below does NOT throw any compiler error, it appears like it should as its similar to what the
above example does. Can anyone explain why it does not?
Meghna Bhardwaj wrote:
The below code throws the compiler error: "Local variable may not have been initialized" on Line 6.
When you declare y in line 3, it is not initialized. And since you are assigning the values to y in "If statement", it is not guaranteed that those lines will be executed . So the error. It is always good pogramming practice to assign variables to 0 in methods, if you are not sure what the variables will carry the value in future.
This is my thought... Pros clarify.
In this, it is guaranteed very line will be executed. So no error.
Thanks
Preparing for SCJP 6
Meghna Bhardwaj
Ranch Hand
Joined: Jun 08, 2007
Posts: 109
posted
0
Hi Madhu,
Thanks for your reply, but in the first code sample looking at the logic it is also
gauranteed that y will be assigned a value, since x =10 and x is either > or <= 100
I think the code the going to enter at least 1 if statement so y will also end up will a value???
The compiler can't always tell whether a local variable has been initialized before use. For example, if you initialize within a logically conditional block (in other words, a code block that may not run, such as an if block or for loop without a literal value of true or false in the test), the compiler knows that the initialization might not happen, and can produce an error.
Because of the compiler-can't-tell-for-certain problem, you will sometimes need to initialize your variable outside the conditional block, just to make the compiler happy. You know why that's important if you've seen the bumper sticker, "When the compiler's not happy, ain't nobody happy."
More reference in K&B: page no 208: Chapter 3: Local (Stack, Automatic) Primitives and Objects.
When you declare a local variable, it must be initialized before it is used.
In this egs,
int aaa;
int x = 0;
if(x == 0)
aaa = 20;
else
aaa = 10;
System.out.println(aaa);
The variable will be initialized either in the if or else statement.
As you said
The compiler can't always tell whether a local variable has been initialized before use. For example, if you initialize within a logically conditional block (in other words, a code block that may not run, such as an if block or for loop without a literal value of true or false in the test), the compiler knows that the initialization might not happen, and can produce an error.
above code compiled properly because there is else block. if you remove else block it won't compile
Thanks
Srikanth
Thanking You,
Srikanth Nakka
Narendhiran Nagarajan
Ranch Hand
Joined: Jun 17, 2009
Posts: 30
posted
0
The compiler will check whether the local variable is initialized before it is used.
So, if you use the "IF statement"(only) and you are initializing the local variable inside the block.
The compiler does not know whether the block will run or not. So, the compiler is uncertain about the initialization of the local var and it will not compile.
But , if you use "IF ELSE statement" and you are initializing the local variable inside both the block(If and Else).
Then the compiler realises that either one of the statement will run for sure and the var will be initialized before its gonna be used.