-1
public class Main{
    public static void main(String[] args) {
        int x;
        int y=10;
        if(y==10){
            x=y;
            //System.out.println("x is "+x);
        }
        System.out.println("x is "+ x);
    }
}

When executed this says x not initialized?Cannot understand why? But the commented print statement is executed and the later one is removed then it works fine?

  • 2
    Welcome to Stack Overflow! The Java compiler will only allow you to access a variable if all branching paths (even those that can't be reached) give that variable a value. Because your `if` statement--even though it always evaluates to true (currently)--has a path where `x` might not be initialized, you cannot use `x` outside of that if statement. Because you are assigning `x` a value inside the if, you can immediately use it in a print statement (because every path to that line gives `x` a value). – Kevin McCarpenter Apr 28 '21 at 04:31

7 Answers7

1

it say that because the print instruction is out of the if block and the compiler doesn't sure that we will enter in the if block to assigne the value of y to x to avoid that error initialize your variable with the value 0

1

If the code does not enter the if then x does not get initialized before it gets to the outer print statement (because the initialization of x is inside the loop).

xagyg
  • 9,562
  • 2
  • 32
  • 29
1

All the other answers are correct, but miss a key element: the difference between a human reader and the compiler.

A human looks at the code and immediately understands that when this code completes, x and y will both be 10.

The compiler could know that, too. It is all literal primitive type values here. A smart compiler could detect the outcome of this code, no need to complain about x not initialized because it IS assigned a value.

But there are many other situations that aren't that clear. Therefore most compilers are very conservative. They rather complain about such code being wrong, to avoid the risk of not complaining about wrong code.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

The compiler produces this issue because there may be case when the if case is not run then no value will be initialized to x so this will produce an error.

Try using int x = 0;

0

Since x=y Is inside the “if” statement, so it’s a conditional assignment of x = 10, the error occurs at the second print statement because it is outside the “if” statement, meaning what if the condition isn’t met and x isn’t assigned the value of y? In that case it’ll be an error. However, if you initialise like int x = 5, in that case it’s an initialisation and NOT a conditional assignment, so there won’t be a compile time error.

Zeeshan Arif
  • 467
  • 4
  • 14
-1

What you are doing is "declaring" a variable. "Initializing" looks like int x = 0;

rajeevbk
  • 55
  • 7
-1

It's not the print that's causing the issues. It's the x=y statement you've written inside the if block.

The variable x is not initialized as the error suggests. Inside the if block, you've set a value for x while you do x=y. So, you've initialized the variable. And so the print works.

Without the x=y block, the print fails because x is not initialized.