• Post Reply Bookmark Topic Watch Topic
  • New Topic

The final field may already have been assigned?  RSS feed

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I don't get this. I have defined a final instance variable:
Since it is not yet assigned to any value, I do so in the constructor:
Why doesn't this code compile at line 7 (The final field may already have been assigned)? Since getLocalHost() may throw an UnknownHostException, at this point the variable hostName can never be assigned any value. Not even an attempt is made.

I am using Eclipse.

Thanks,
Kjeld
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler can't determine that 'hostName' will be initialized only once if ever. Conversely it is smart enough to figure out that it might not have been initialized at all if an exception should occur.
You can work around this using a temporary local variable. More on this here: http://www.ibm.com/developerworks/java/library/j-jtp1029.html

 
Kjeld Sigtermans
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I used the 'thingie' example because I like hostName to be final.
However I still think it is awkward.

Cheers, K
 
Sheriff
Posts: 21136
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way is use a (private) method:
 
Kjeld Sigtermans
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok to my taste that's a little more elegant since there is no need for an explicit temp variable.
Still, I wonder why a compiler could not determine if a value has yet been assigned to the variable.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you didn't miss out a "not" from that error message.

It is impossible at compile-time to predict whether an exception will or will not occur. The compiler queries all possible paths of execution through the entire method, and sees whether there are any where the field is not assigned to.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Are you sure you didn't miss out a "not" from that error message.

It is impossible at compile-time to predict whether an exception will or will not occur. The compiler queries all possible paths of execution through the entire method, and sees whether there are any where the field is not assigned to.


The compiler is not complaining that the field may not have been assigned a value. It is complaining that it is possible it may have been assigned a value twice - once in the try block and once in the catch block. It doesn't realise that the line in the try block that throws the exception is also the line that assigns the value.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!