In Java if we provide following statement, it will produce compilation error:
class A {
public static void main(String[] args){
int i = 10;
int j;
if(i == 10) {
j = 20;
}
System.out.println(j);
}
}
But in the following statement, no compilation error:
class A{
public static void main(String[] args){
int i = 10;
int j;
if(i == 10) {
j = 20;
}
else {
j = 30;
}
System.out.println(j);
}
}
Neither this produces compilation error:
class A{
public static void main(String[] args) {
final int i = 10;
int j;
if(i == 10){
j = 20;
}
System.out.println(j);
}
}
What is the reason?