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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Local variable Initialization confusion" Watch "Local variable Initialization confusion" New topic
Author

Local variable Initialization confusion

Meghna Bhardwaj
Ranch Hand

Joined: Jun 08, 2007
Posts: 109
HI All,

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?
Madhu Desai
Ranch Hand

Joined: Jun 14, 2009
Posts: 42
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
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???
Gerin Jacob
Greenhorn

Joined: Jun 11, 2009
Posts: 8

Your thought is right - but applicable to humans only.
Java wont look into the complete code before compiling it.
It takes each line and then decides.

Regards,
Gerin Jacob


Regards,
Gerin Jacob
Madhu Desai
Ranch Hand

Joined: Jun 14, 2009
Posts: 42
Meghna Bhardwaj wrote:Hi Madhu,

I think the code the going to enter at least 1 if statement so y will also end up will a value???


Yes its true. But some how java follows that way.

Here is little excerpt from K&B:

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.
Venkata Saraswathi
Ranch Hand

Joined: Sep 27, 2008
Posts: 55


FYI, If you try as above it will work.
Narendhiran Nagarajan
Ranch Hand

Joined: Jun 17, 2009
Posts: 30
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.

Naren
-------------------
SCJP - 90%
SCWCD - 98%
Srikanth Nakka
Greenhorn

Joined: Apr 15, 2007
Posts: 26

Hi Narendhiran,


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
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.

Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

Hi All,
Also see explaination given in Java Language Specification 3.0.


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
 
Don't get me started about those stupid light bulbs.
 
subject: Local variable Initialization confusion