The variable may not have been initialized with an 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 the following 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 is going wrong?

+29
java variables initialization
Mar 15 '10 at 16:45
source share
11 answers

You declared them, but you did not initialize them. Initializing them sets them equal to the value:

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

You get an error because you did not initialize the variables, but you increment them (e.g. a++ ) in a for loop.

+52
Mar 15
source share

Local variables do not get default values. Their initial values ​​are undefined, without assigning values ​​in any way. Before you can use local variables, they must be initialized.

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

If you declare a field at the class level, they get the default values ​​according to their type. If you declare a variable at the method level or as a block (means that anycode inside {}) does not receive any values ​​and remains undefined until somehow they get some initial values, that is, some values ​​assigned to them.

+51
Jan 6 '13 at 9:50
source share

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

You are a little 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". This is because a special constructor will be created when creating a new instance of Clazz. First, super () will be called, then the field a will be initialized implicitly, and then the line b = 0 will be executed.

+5
Mar 15
source share

You have declared them, but not initialized.

 int a; // declaration, unknown value a = 0; // initialization int a = 0; // declaration with initialization 
+3
Mar 15
source share

You declared them, but did not initialize them with a value. Add something like this:

 int a = 0; 
+2
Mar 15
source share

You did not initialize a and b , only declared them. There is a subtle difference.

 int a = 0; int b = 0; 

At least this is for C ++, I suppose Java is the same concept.

+1
Mar 15
source share

You declared them at the beginning of the method, but you never initialized them. Initialization will set them equal to the value, for example:

 int a = 0; int b = 0; 
+1
Mar 15
source share

You declared them, but did not indicate their original value - thus, they are not initialized. Try something like:

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

and warnings should disappear.

+1
Mar 15 '10 at 16:48
source share

Imagine what happens if x [l] is neither 0 nor 1 in the loop. In this case, a and b will never be assigned and are undefined. You must initialize them as with some value, for example 0.

+1
Mar 15 '10 at 16:48
source share

Set the variable "a" to some value like this,

 a=0; 

Declaration and initialization are different.

Luck

+1
Jun 28 '16 at 4:38 on
source share
 int a = 0; int b = 0; 
0
Mar 15
source share



All Articles