Java: Error: Variable Might Not Have Been Initialized

Variable might not have been initialized error

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

variable result might not have been initialized

Your first line should be:

int n1, n2, result=0;

result variable was not initialized nor it could be calculated from the code, hence the compiler thinks it won't find the value of the variable. Thus its giving compilation error.

java variable scope, variable might not have been initialized

In Java local variables are not initialized with a by default value (unlike, for example field of classes). From the language specification one (§4.12.5) can read the following:

A local variable (§14.4, §14.14) must be explicitly given a value
before it is used
, by either initialization (§14.4) or assignment
(§15.26), in a way that can be verified using the rules for definite
assignment (§16 (Definite Assignment)).

Because it is explicitly set on the Java language specification the compiler will not try to (and should not) infer that the variable c will always be updated inside the loop:

public class HelloWorld {
public static void main(String[] args) {
int c;
for (int i=0; i <5; i++) {
System.out.println(i);
c = 100;
}
System.out.println(c);
}
}

The compiler strictly enforces the standard and notifies you about having breaking one of its rules with the error:

"variable c might not have been initialized"

So even though your code can be formally proven to be valid, it is not the compiler job to try to analyze your application's logic, and neither does the rules of local variable initialization rely on that. The compiler checks if the variable c is initialized according to the local variable initialization rules, and reacts accordingly (e.g., displaying a compilation error for the case of int c;).

How to avoid variable might not have been initialized near try/catch with System.exit

The oddity here is that the compiler doesn't know that System.exit(1); will never return. If it knew that, it would be happy.

So all you need to do is give it something that it knows won't let you get from the catch block to after the try/catch. For example:

try {
foo = Long.parseLong(args[0]);
bar = Long.parseLong(args[0]);
} catch (NumberFormatException e) {
System.out.println(USAGE);
System.exit(1);
throw new RuntimeError("Make sure the end of the catch block is unreachable");
}

If you need to do this often, you might want to write a helper method, and throw the result of it (which you'll never use). That way you still only have a single line of code for "I want to quit now".

try {
foo = Long.parseLong(args[0]);
bar = Long.parseLong(args[0]);
} catch (NumberFormatException e) {
System.out.println(USAGE);
throw HelperClass.systemExit(1);
}

...

public class HelperClass {
public static RuntimeException systemExit(int exitCode) {
System.exit(1);
throw new RuntimeException("We won't get here");
}
}

Another option I've used quite a bit is to define a sort of "User error" exception. You can then catch that at the top level (in main), print any message and possibly display the usage. That way:

  • You can unit test user errors (unit testing System.exit is at least more awkward)
  • You have centralized handling of "what do I want to do if the user made an error" rather than including System.out.println(USAGE) in multiple places
  • You don't run into this definite assignment issue

So your code would then be:

try {
foo = Long.parseLong(args[0]);
bar = Long.parseLong(args[0]);
} catch (NumberFormatException e) {
throw new UserInputException("foo and bar must both be valid integers");
}


Related Topics



Leave a reply



Submit