0

I tried compiling this code in Java:

class D
{
    public static void main(String arg[])
    {
        f1();
    }

    static void f1()
    {
        int a;
        int b=5;
        for(;b<=10;b++)
            a=b;
        System.out.println(a);
    }
}

But it generates an error that says: Variable 'a' might not have been initialized. Why is this happening, although a is set in the for loop?

Drunix
  • 3,313
  • 8
  • 28
  • 50
Pinky
  • 88
  • 6
  • 4
    Yes, you didn't initialize `a`. – devnull Mar 27 '14 at 10:11
  • possible duplicate of [Variable might not have been initialized error](http://stackoverflow.com/questions/2448843/variable-might-not-have-been-initialized-error) – Drunix Mar 27 '14 at 10:31
  • Ok, this might be not a real duplicate because of the loop. Maybe you could emphasize that point in your question? – Drunix Mar 27 '14 at 10:44
  • Thank You @Drunix, I will keep this in mind when asking another question. – Pinky Mar 27 '14 at 10:46

4 Answers4

5

The loop might not run (the compiler doesn't know for sure) and thus a might not be initialized.

This doesn't execute the print statement in the loop, just the assignment:

for(;b<=10;b++)
  a=b;
System.out.println(a); //this will only run after the loop.

In the above case if b was > 10 before the loop then a=b; would never be executed and the print statement would get un initialized a.

I assume you mean this instead:

for(;b<=10;b++) {
  a=b;
  System.out.println(a);
}

If you intend to print a after the loop, initialize it to whatever value is appropriate, e.g. int a = 0;.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Thomas
  • 87,414
  • 12
  • 119
  • 157
  • Yeah, this is what i was trying to understand actually. I didn't initialize 'a' on purpose and the solution you have told me, it has been giving me correct output. But i am totally confused about the working of the loop, i still can't understand why does it behave this way. Each time the loop iterates, it should give the value of 'b' to 'a'. Does this not happen? Please elaborate. – Pinky Mar 27 '14 at 10:25
  • @user3467998 yes it does but the compiler doesn't know whether the loop will iterate at all, i.e. it doesn't know whether `b` will match the condition or not (the compiler isn't that clever and making it do so would add much complexity for almost no benefit). – Thomas Mar 27 '14 at 10:29
  • Thank you So much for clearing my confusion. I has tried this with an 'if' condition as well instead of the 'for' loop and compiler was telling me the same thing. But when i added an 'else' part, the program compiled fine. The Code for 'f1()' with an 'if' would be: if(b<=10) a=5; System.out.println(a); – Pinky Mar 27 '14 at 10:35
  • @user3467998 yes, assuming the both the _if_ and _else_ parts would initialize `a` the compiler would know that one branch would be executed and `a` would be guaranteed to be initialized when execution reaches the print statement. – Thomas Mar 27 '14 at 10:41
  • Yes Sir, I got the point! Thanks a lot for the timely help @Thomas – Pinky Mar 27 '14 at 10:48
2

Variables inside method bodies are not assigned default values like they would as instance fields, etc.

Therefore your int a declaration, which does not initialize a with a value, generates your compile error.

Assign a with a default value to get rid of this.

int a = 0;

... or declare it outside the method body:

static int a;

static void f1() {
    ...
    System.out.println(a); // no errors
}
Mena
  • 47,782
  • 11
  • 87
  • 106
2

The static error check isn't that clever. It doesn't know that your loop will always run, therefore you may output 'a' without initializing it.

That type of issue is only picked up at run time, so it warns you.

LordSquall
  • 161
  • 7
0

Initialise variable

int a = 0;

because a is local variable and local variable is created on stack and variable created on stack memory does not assign any default value. So we must have to initialize local variable.

Harshal Pathak
  • 757
  • 1
  • 6
  • 18