Variable might not have been initialized error

457,683

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

You declared them, but not initialized.

int a; // declaration, unknown value
a = 0; // initialization
int a = 0; // declaration with initialization

Solution 5

You declared them, but you didn't initialize them with a value. Add something like this:

int a = 0;
Share:
457,683

Related videos on Youtube

David
Author by

David

I'm David.

Updated on July 05, 2022

Comments

  • David
    David almost 2 years

    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?

  • Arun
    Arun about 14 years
    Perhaps "int b = 0;" is "declaration and initialization."
  • jww
    jww over 7 years
    For future visitors... Also see Default Values and Initialization in Java when the potentially uninitialized variable is a class member.
  • Srujan Barai
    Srujan Barai over 6 years
    Can somebody explain how is this possible because primitives cannot have null values, their default value is 0, why does it show not initialized error?
  • user207421
    user207421 over 6 years
    Not really. Initialization is an optional part of the declaration syntax.
  • user207421
    user207421 over 6 years
    @SrujanBarai Their default value is zero when declared as class members. Local variables don't have default values.
  • sziraqui
    sziraqui over 5 years
    static members probably get default values in some other way.
  • Veco
    Veco over 4 years
    What about if "a" is a generic?
  • Peter Mortensen
    Peter Mortensen over 3 years
    You can't derive it from C++. Local variables in Java don't automatically get default values (but could have if Java had been designed differently), but instance and class variables ("static") do get default values.