Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: variable might already have been assigned. Why use the word "might"?

When I run the javac compiler on the following code -

void method() {
    final int x;
    x = 1;
    x = 1;  // (Intentional error)
}

I receive the following error -

..\src\pkgs\main\Main.java:60: error: variable x might already have been assigned
                x = 1;  // (Intentional error)
                ^

I wonder why this error message uses the word "might". Would a more accurate description use the word "has" instead, as in "has already been assigned"? Is there a particular reason why the compiler seems to use a kind of "vague description style" for this type of error? Thanks.

like image 315
user2911290 Avatar asked May 04 '14 09:05

user2911290


People also ask

Why does it say variable might not have been initialized?

2. Java Error: “variable might not have been initialized” Should we declare a local variable without an initial value, we get an error. This error occurs only for local variables since Java automatically initializes the instance variables at compile time (it sets 0 for integers, false for boolean, etc.).

What happens when the local variable is not initialized?

In C and C++, local variables are not initialized by default. Uninitialized variables can contain any value, and their use leads to undefined behavior. Warning C4700 almost always indicates a bug that can cause unpredictable results or crashes in your program.

What does Cannot be resolved to a variable mean in Java?

Fix the cannot be resolved to a variable Error in Java If you try, the cannot be resolved to a variable error will come out. It means it cannot detect the initialization of variables within its scope. Similarly, if you make a private variable, you cannot call it inside a constructor. Its scope is out of bounds.


2 Answers

A final variable can only be assigned if it is definitely unassigned. That is, "might" is referring to the fact that the variable is not definitely unassigned:

Similarly, every blank final variable must be assigned at most once; it must be definitely unassigned when an assignment to it occurs.

For instance, consider this code which makes the wording of "might" more clear:

final int x;
if (maybeTrueOrMaybeFalse()) {
   x = 1;
}
/* error: variable x might already have been assigned */
x = 1;

The standard Sun/Oracle javac compiler produces the same error message for the "might" (not definitely unassigned) and "has been" (definitely assigned) cases. A different compiler or code analysis tool could very well provide a different/refined message in the "has been" case.

like image 82
user2864740 Avatar answered Sep 20 '22 07:09

user2864740


If you have initialized it when declaring, it would give you a precise message, e.g.

"The final local variable x cannot be assigned. It must be blank and not using a compound assignment"

final int x = 6;
// code
x = 4;

But, in your example, the final variable might have been initialized somewhere after it is declared and the compiler doesn't keep track of consecutive statements, otherwise it would be named "runner" and it just has the information that final variable x should not be initialized twice.

like image 42
Juvanis Avatar answered Sep 19 '22 07:09

Juvanis