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?

4
  • 4
    Yes, you didn't initialize a. Commented Mar 27, 2014 at 10:11
  • possible duplicate of Variable might not have been initialized error Commented Mar 27, 2014 at 10:31
  • Ok, this might be not a real duplicate because of the loop. Maybe you could emphasize that point in your question? Commented Mar 27, 2014 at 10:44
  • Thank You @Drunix, I will keep this in mind when asking another question. Commented Mar 27, 2014 at 10:46

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
@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).
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);
@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.
Yes Sir, I got the point! Thanks a lot for the timely help @Thomas
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
}

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.