Tuesday, November 4, 2008

Published 11/04/2008 by with 0 comment

variable … might not have been initialized

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);
}
}
    email this       edit

0 comments:

Post a Comment