• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

The final field may already have been assigned?

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another way is use a (private) method:
 
Kjeld Sigtermans
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic