Tuesday, November 4, 2008



We see “variable might not have been initialized” when we try to use the value of a variable before assigning any value to that variable. Each local variable must be assigned a value before further using it.

Here is an example of this error :
public class VariableMightNotHaveBeenInitialized
{
public static void main(String args[])
{
int a,b,c;
a=10;
c=a+b;
System.out.println("The sum is "+c);
}
}


Here variable ‘b’ is added with variable ‘a’ before assigning any value.

Correct code is:
public class VariableMightNotHaveBeenInitializedSolution
{
public static void main(String args[])
{
int a,b,c;
a=10;
b=20;
c=a+b;
System.out.println("The sum is "+c);
}
}


If the above solutions do not work for you. Please comment below giving your code. I'll try to respond.

0 comments:

Post a Comment