Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable might not have been initialized error

When I try to compile this:

public static Rand searchCount (int[] x)
{
    int a ;
    int b ;

    ...

    for (int l= 0; l<x.length; l++)
    {
        if (x[l] == 0)
        a++ ;
        else if (x[l] == 1)
        b++ ;
    }

    ...

}

I get these errors:

Rand.java:72: variable a might not have been initialized
                a++ ;
                ^
Rand.java:74: variable b might not have been initialized
                b++ ;
                ^
2 errors

It seems to me that I initialized them at the top of the method. What's going wrong?

like image 280
David Avatar asked Mar 15 '10 16:03

David


People also ask

Why does it say variable might not have been initialized?

2. Java Error: “variable might not have been initialized” Should we declare a local variable without an initial value, we get an error. This error occurs only for local variables since Java automatically initializes the instance variables at compile time (it sets 0 for integers, false for boolean, etc.).

What happens if you don't initialize a variable Java?

Declaring final variable without initialization If you declare a variable as final, it is mandatory to initialize it before the end of the constructor. If you don't you will get a compilation error.

What happens when the local variable is not initialized?

If the programmer, by mistake, did not initialize a local variable and it takes a default value, then the output could be some unexpected value. So in case of local variables, the compiler will ask the programmer to initialize it with some value before they access the variable to avoid the usage of undefined values.


3 Answers

You declared them, but you didn't initialize them. Initializing them is setting them equal to a value:

int a;        // This is a declaration
a = 0;        // This is an initialization
int b = 1;    // This is a declaration and initialization

You get the error because you haven't initialized the variables, but you increment them (e.g., a++) in the for loop.

Java primitives have default values but as one user commented below

Their default value is zero when declared as class members. Local variables don't have default values

like image 165
mipadi Avatar answered Oct 23 '22 19:10

mipadi


Local variables do not get default values. Their initial values are undefined with out assigning values by some means. Before you can use local variables they must be initialized.

There is a big difference when you declare a variable at class level (as a member ie. as a field) and at method level.

If you declare a field at class level they get default values according to their type. If you declare a variable at method level or as a block (means anycode inside {}) do not get any values and remain undefined until somehow they get some starting values ie some values assigned to them.

like image 25
reddy Avatar answered Oct 23 '22 19:10

reddy


If they were declared as fields of the class then they would be really initialized with 0.

You're a bit confused because if you write:

class Clazz {
  int a;
  int b;

  Clazz () {
     super ();
     b = 0;
  }

  public void printA () {
     sout (a + b);
  }

  public static void main (String[] args) {
     new Clazz ().printA ();
  }
}

Then this code will print "0". It's because a special constructor will be called when you create new instance of Clazz. At first super () will be called, then field a will be initialized implicitly, and then line b = 0 will be executed.

like image 9
Roman Avatar answered Oct 23 '22 19:10

Roman