0

In this code there is a compile time error.

"Example.java:17: error: variable y might not have been initialized".

Can anyone explain what's the reason for this error and how to fix it?

import java.util.*;
class Example{
  public static void main (String args[]){
    Scanner input=new Scanner(System.in);
    System.out.println("Input an integer : ");
    int num=input.nextInt();
    int y;
    if(num>100){
      y=200;
    }
    if(num<100){
      y=200;
    }
    if(num==100){
      y=200;
    }
    System.out.println(y);
    
  }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 3
    you only initialize y in conditional blocks. your compiler will not go and check all your conditions to see if all scenario's are covered. – Stultuske Jan 17 '22 at 13:57
  • 3
    Convert that series of `if`s to `if-else if-else` – Federico klez Culloca Jan 17 '22 at 13:57
  • 1
    a simple `int y = 0;` solves the issue. The compiler does not know that your 3 `if`s cover all the cases. – f1sh Jan 17 '22 at 13:58
  • The essential thing to understand: a compiler isn't a human. Albeit the compiler **could** figure out here that there is no path to the print *without* assigning a value to y ... well, it just doesn't. – GhostCat Jan 17 '22 at 14:05
  • @FedericoklezCulloca, thanks. it worked in if , else if and else series. but I relly need to understand why it works in if, if else , else and doesn't work in if series. because all the scenarios were covered in if series. – vichakshana deegau Jan 17 '22 at 14:13
  • @Stultuske thanks. but when I used if, else if, else it executed. if compiler doesn't check all of that cases, how did it execute? That's what I'm looking answers for. – vichakshana deegau Jan 17 '22 at 14:19
  • @vichakshanadeegau because that's how [the language is defined](https://docs.oracle.com/javase/specs/jls/se17/html/jls-16.html). In particular see example 16-2. The compiler does not consider the possible values of `num`, only the structure of your code. – Federico klez Culloca Jan 17 '22 at 14:20
  • @GhostCat, thanks, then how did it executed when using a if, else if and else? – vichakshana deegau Jan 17 '22 at 14:23
  • 1
    @vichakshanadeegau because of the difference between if and else. Now: if ( a > x || a == x || a < x ) -> this also covers all the possibilities, but the compiler won't know. if ( a > x ) { doOneThing(); } else { } -> always covers everything – Stultuske Jan 17 '22 at 14:27
  • @Stultuske strait to the point.. :-) thanks it helped a lot. – vichakshana deegau Jan 17 '22 at 14:37

1 Answers1

2

Before accessing a variable it must be initialized.

Static and instance variables are initialed implicitly even if you are not providing a value while declaring them. But with local variables, it's not the case, they must be assigned explicitly before you can use them.

You are not using else statement and it's not obvious to the compile that you've covered all possible cases.

Try this instead, you'll see that now variable y changes accordingly to the user input:

        int y;
        if (num < 100) {
            y=100;
        }
        else if (num > 100) {
            y=200;
        }
        else {
            y=300;
        }
Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46