Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring local variables as final without initializer and assigning in if-statement

I just made a small code change to silence a FindBugs warning which required moving some code to an anonymous inner class. In order to access some variables, I had to declare those as final. So this is the code snippet after the change:

final File[] libPath; // libPath is final but assignment takes place later
if (libraryPath != null) {
    libPath = pathToFiles(libraryPath);
} else {
    libPath = new File[0];
}

This compiles just fine with language set to Java 6 in current Eclipse (Version 3.7.1). However I'm quite sure this used to give an error in some previous version. Seems the compiler accepts this construct when it can determine that there will be.

My question is: is this legal in Java 6 or is it something that now works due to a side effect of Java 7 support being added to eclipse 3.7.1? We have seen such side effects with certain usage of generics that works in 3.7.1 but didn't compile in 3.7.0.

like image 748
Axel Avatar asked Jan 18 '12 14:01

Axel


People also ask

Can we declare final variable without initialization?

Declaring final variable without initialization If you declare a final variable later on you cannot modify or, assign values to it. Moreover, like instance variables, final variables will not be initialized with default values. Therefore, it is mandatory to initialize final variables once you declare them.

Can you declare variables in an if statement?

Java allows you to declare variables within the body of a while or if statement, but it's important to remember the following: A variable is available only from its declaration down to the end of the braces in which it is declared.

Can we declare local variable as final?

final is the only allowed access modifier for local variables. final local variable is not required to be initialized during declaration. final local variable allows compiler to generate an optimized code. final local variable can be used by anonymous inner class or in anonymous methods.

Can we use local variable without assigning a value?

Although the correlator automatically initializes global variables that were not explicitly assigned a value, the correlator does not do this for local variables. For local variables, you must explicitly assign a value before you can use the variable.


1 Answers

this is ok. it is called blank final

quote from wiki:

A final variable can only be initialized once, either via an initializer or an assignment statement. It need not be initialized at the point of declaration: this is called a "blank final" variable. A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared: otherwise, a compile-time error occurs in both cases. [4] (Note: If the variable is a reference, this means that the variable cannot be re-bound to reference another object. But the object that it references is still mutable, if it was originally mutable.)

Blank final

The blank final, which was introduced in Java 1.1, is a final variable whose declaration lacks an initializer. [5][6] A blank final can only be assigned once and must be unassigned when an assignment occurs. In order to do this, a Java compiler runs a flow analysis to ensure that, for every assignment to a blank final variable, the variable is definitely unassigned before the assignment; otherwise a compile-time error occurs.[7]

In general, a Java compiler will ensure that the blank final is not used until it is assigned a value and that once assigned a value, the now final variable cannot be reassigned another value.[8]

link: http://en.wikipedia.org/wiki/Final_%28Java%29

like image 139
Kent Avatar answered Oct 04 '22 04:10

Kent