user2954714
user2954714

Reputation: 1

Using objects/variables initialized inside a loop?

I declare my objects outside of an if statement, but initialize them inside of it. Later I try and use them but it won't allow it because they haven't been initialized. The if statements are structured so that they all will execute before the objects are used, and hence the objects will be initialized guaranteed, but java doesn't allow that. Any way around this?

Upvotes: 0

Views: 653

Answers (4)

Jason C
Jason C

Reputation: 40315

If you know that all paths will ultimately initialize them, but the compiler doesn't, you can initialize them to null or 0 or false (as ajb helpfully reminds) -- or some other special initial value you define -- when declaring them. They then have a concrete initial value.

If the variable is still null (or whatever it's initial value was) by the time you use it (evidenced perhaps by an NPE in the case of an object) then you know something went wrong; you can also self-document your assumptions with asserts later.

You should post your code so we can give you better advice; the compiler is relatively smart about path analysis, although it can't, of course, handle cases that rely on external input or assumed preconditions and invariants. Still, it's always possible that you've overlooked something (perhaps an exception or unexpected condition leads to a path where the value is uninitialized - which is fine, you just have to make sure it's initialized).

Upvotes: 2

Hot Licks
Hot Licks

Reputation: 47699

Java requires that a variable be initialized along all possible paths to the point of use before its value can be referenced. Eg, if you have

int x;
if (a == b) {
   x = 5;
}
if (c == d) {
   x = 6;
}
int y = x;

In the above case the compiler cannot know that either the first or second if statement will be true, and so it's not certain that x gets assigned a value along all paths leading to the assignment to y. So the compiler will disallow this (and, if the compiler didn't reject this, the "verifier" inside the JVM would).

The solution is to assign a value to the variable (maybe zero or -1 in this case, null for an object reference) so that it's known to have a value along all paths.

But note that you probably should not just assign a dummy value to every variable you declare, since very often the compiler message that no value is assigned can indicate a real live code bug where you've accidentally omitted assigning a value to the variable along some path.

Upvotes: 1

Makoto
Makoto

Reputation: 106389

The only way an object's initialization is guaranteed after a conditional expression is if there exists a branch that is always executed, such as an else statement, or default in switch statements.

To decompose that, take this example code:

String word;
String name = "Peter";
if("Peter".equals(name)) {
    word = "The Bird";
}
System.out.println(word);

This will fail since the compiler identifies a branch in which word is not initialized.

If you add an else clause, then the compiler will believe that word is initialized.

String word;
String name = "Peter";
if("Peter".equals(name)) {
    word = "The Bird";
} else {
    word = "Nope";
}
System.out.println(word);

Upvotes: 1

Taylor
Taylor

Reputation: 4087

initialize them to null

Object myAwesomesauceVariableOfAwesome = null;
if(myUnbelievablyWildBoolean){
    myAwesomesauceVariableOfAwesome = getAwesomesauce();
}
doSomethingCompletelyMindBlowingWithAwesomesauce(myAwesomesauceVariableOfAwesome);

Upvotes: 0

Related Questions