What is the output of this program?
class Variable8
{
public static void main(String[] alt)
{
int i=10;
int j;
if(i==10)
j=20;
System.out.println(j);
}
}
/*
Output:- CE[variable j might not have been initialized]
Explanation:- First of all variable "j" is a local variable and it is not initialized while
declaring. In this case compiler cannot guess that if statement will be true or
false at runtime. and variable "j" will be initialized or not. Because compiler
can guess only final variable. Hence due to ambiguity compiler will not take any
risk and rise compilation error.
*/
{
public static void main(String[] alt)
{
int i=10;
int j;
if(i==10)
j=20;
System.out.println(j);
}
}
/*
Output:- CE[variable j might not have been initialized]
Explanation:- First of all variable "j" is a local variable and it is not initialized while
declaring. In this case compiler cannot guess that if statement will be true or
false at runtime. and variable "j" will be initialized or not. Because compiler
can guess only final variable. Hence due to ambiguity compiler will not take any
risk and rise compilation error.
*/
0 comments: