• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Local variable Initialization confusion

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Greenhorn
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Madhu Desai
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


FYI, If you try as above it will work.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 26
Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Narendhiran,


above code compiled properly because there is else block. if you remove else block it won't compile

Thanks
Srikanth
 
Narendhiran Nagarajan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Also see explaination given in Java Language Specification 3.0.
 
reply
    Bookmark Topic Watch Topic
  • New Topic