annoying error: local variable may not have been initialized

S

Shawn

Hi,

I am writing a program and I run into a lot of annoying errors: local
variable may not have been initialized. For example:

public class MyClass
{
public void aMethod()
{
double number; //declare a local variable
...//code
if (number > 0) //Error: the local variable may not have been
initialized
{
...//code
}
...//code
return;
}
}

How to avoid such errors? Maybe initialized to 0:

double number = 0.0;

will get rid of the error. But my program has about 30 local variables.
I am using:

double num1, numABC, iX, ..., w;

I hate to do the following:

double num1 = 0.0;
double numABC = 0.0;
double iX = 0.0;

I think such message should be a warning, instead of error. Of course,
the variable has at least a default value.
 
Y

Yan

I hate to do the following:
double num1 = 0.0;
double numABC = 0.0;
double iX = 0.0;
Why?



I think such message should be a warning, instead of error. Of course,
the variable has at least a default value.

local variables are not automatically initialized in Java, and if not
initialized explicitely will contain garbage. The fact that Java
compiler catches that turns your potentially run-time errors into
compile times ones, and the common wisdom is that former is much worse
than the latter.
 
C

Christopher Benson-Manica

Shawn said:
I am writing a program and I run into a lot of annoying errors: local
variable may not have been initialized. For example:
(snip)

How to avoid such errors? Maybe initialized to 0:

Yes, or initialized to *something*. As in many languages, local
variables contain garbage unless you initialize them. Java is
actually being very helpful by telling you so at compile time rather
than taking the "programmer-knows-best" approach a C compiler would.
double number = 0.0;
will get rid of the error. But my program has about 30 local variables.

Well, you get to initialize all of them then. The amount of time that
takes is guaranteed to be much less than you would spend debugging the
random errors that would otherwise result.
I am using:
double num1, numABC, iX, ..., w;

double num1=0.0D, numABC=0.0D...

if you insist on declaring everything on one line.
I think such message should be a warning, instead of error. Of course,
the variable has at least a default value.

Not in this language. *Class* variables get default values, but local
variables do not.
 
S

Shawn

Yan said:
local variables are not automatically initialized in Java,

I am not aware of this. Are you sure?

By the way, for my education purpose, how about a local array, say

int[] aArray = new int[10];

No need to initialize every item to 0, right?
 
S

Shawn

Christopher said:
Yes, or initialized to *something*. As in many languages, local
variables contain garbage unless you initialize them. Java is
actually being very helpful by telling you so at compile time rather
than taking the "programmer-knows-best" approach a C compiler would.



Well, you get to initialize all of them then. The amount of time that
takes is guaranteed to be much less than you would spend debugging the
random errors that would otherwise result.



double num1=0.0D, numABC=0.0D...

if you insist on declaring everything on one line.


Not in this language. *Class* variables get default values, but local
variables do not.

I see. Thank you all.
 
Y

Yan

Shawn said:
Yan said:
local variables are not automatically initialized in Java,

I am not aware of this. Are you sure?

By the way, for my education purpose, how about a local array, say

int[] aArray = new int[10];

No need to initialize every item to 0, right?

Right, no need to explicitely set every array item to 0. But that
probably has more to do with array initialization rather than with
local variables.
 
M

Mike Schilling

Shawn said:
I am not aware of this. Are you sure?

Yes, it's well-known.
By the way, for my education purpose, how about a local array, say

int[] aArray = new int[10];

No need to initialize every item to 0, right?

Yes, because the members of the array aren't local variables, they're part
of the array object .
 
T

Tor Iver Wilhelmsen

Shawn said:
int[] aArray = new int[10];

No need to initialize every item to 0, right?

Correct, because array elements are initialized by the "constructor".
The local variable here is aArray, which you initialize explicitly.
 
T

Thomas Hawtin

Shawn said:
By the way, for my education purpose, how about a local array, say

int[] aArray = new int[10];

No need to initialize every item to 0, right?

It would be useful to statically check whether the elements of the array
have been initialised before use. However, there isn't a sensible way of
the language declaring the rules. Not least because a reasonable
programmer would use library methods, such as Arrays.fill, to manipulate
the array contents. Much the same argument goes for fields. As it is,
definite assignment/unassignment takes up one very dull chapter of the
JLS (actually it isn't so bad once you get into it).

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top