I am unable to find the reason why I am getting:
variable might not have been initialized
Example 1:
class Test {
public static void main(String[] args) {
int i = 10;
int j;
if (i == 10) {
j = 20;
}
System.out.println(j);
}
}
Test.java:11: error: variable j might not have been initialized System.out.println(j); ^ 1 error
Example 2:
class Test {
public static void main(String[] args) {
int i = 10;
int j;
if (i == 10) {
j = 20;
} else {
j = 30;
}
System.out.println(j);
}
}
Output:
20
My doubt is in the second example, how j
is initialized?