mardi 27 avril 2021

Why does this code say x not intialized but when we execute print statement inside if statement it works fine?

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?

Aucun commentaire:

Enregistrer un commentaire