1

I get the "The local variable string may not have been initialized" error with the following code. The code itself doesn't make sense it was written just for the sake of exercise.

public class StringExercise
{
    public static void main(String[] args)
    {
        String string; // initializing here fixes the issue
        for (int i = 0; i < 10; ++i)
        {
            if( (i % 4) == 2 )
            {
                string = "Number: " + i;
            }
        }
        System.out.println(string); // this is marked as wrong by Eclipse
    }
}

To get it working it is sufficient to initialize String as expressed in the comment above.

My question is why is it needed? The method println will never be given null and initialization will happen the first time the condition in the loop returns true. Am I doing something wrong or is it just Java being overcautious over programmer's errors? If the latter, how is it justified from the theoretical point of view?

infoholic_anonymous
  • 969
  • 3
  • 12
  • 34
  • http://stackoverflow.com/questions/415687/why-are-local-variables-not-initialized-in-java – BatScream Nov 17 '14 at 23:19
  • 1
    Every Java local variable must be *guaranteed* to be assigned a value (even if only `null`) before it's referenced. – Hot Licks Nov 17 '14 at 23:20
  • 1
    @BatScream I know it is not initialized, the question is different. – infoholic_anonymous Nov 17 '14 at 23:20
  • 1
    Every Java local variable must be *guaranteed* to be assigned a value (even if only null) before it's referenced. – Hot Licks Nov 17 '14 at 23:21
  • This isn't to prevent stupid programmer errors, it's so that Java can guarantee it's own type safety. There must be an assignment (of the same type) "visible" along *every* possible path to the point of reference. – Hot Licks Nov 17 '14 at 23:23

2 Answers2

7

My question is why is it needed?

Because even though your code is "logically" written so that string will indeed be initialized in the loop, the compiler doesn't know it. All it sees is:

for (loop; elements; here)
    if (someCondition)
        string = something;

In short: a compiler will not check the logic of your code; it it only smart enough as to check for syntax errors, but after that, the bytecode generation itself is "dumb".

And as Java requires that all variables be initialized before use, you get this compiler error.

fge
  • 119,121
  • 33
  • 254
  • 329
  • But that wouldn't be a problem for a c++ compiler for example. – infoholic_anonymous Nov 17 '14 at 23:21
  • 2
    C++ (and C for that matter) has a different take on `auto` variables; if they are not initialized, they have a value which is not determined. Java _requires_ that local variables are initialized before use. – fge Nov 17 '14 at 23:22
  • @HotLicks I am afraid I fail to understand the type safety aspect you mention; can you give an example? – fge Nov 17 '14 at 23:26
  • Java must *guarantee* that the value arriving at, say, an operation that references a String is indeed a String. This requires that the value be set along all paths. – Hot Licks Nov 18 '14 at 00:21
1

The compiler can't guarantee that string = "Number: " + i; will be executed within your for and if.

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257