views:

66

answers:

1

I would like to know if there's a compiler option that could allow me to remove/cure the error that comes up ("variable X might not have been initialized") when I compile a class who has a final field in it. Or even better would be to have the final fields initialized to the default java value.

Thanks,

ExtremeCoder

+3  A: 

The way to cure the error is fix the code: make sure you initialize the value explicitly. I don't believe there's any option to just ignore the error.

One way of mimicking this is to give the constructor local variables set to the Java default values, then replace them during the course of the constructor. At the end of the constructor, set the field values to those of the local variables.

Mind you, if you've got a lot of code in your constructor you might want to consider refactoring anyway...

Jon Skeet
Thanks, I think I might try your second idea. The problem is that I have over 1000 generated source files and I don't want to go over each one.Maybe I should have added that I am actually using JAXB to generate these and I am removing the setter methods and making the fields final. Now that means that the fields are being initialized by the unmarshalling process (that's why I would like to to just get rid of the error together as it is insignificant anyway...Thanks though.
ExtremeCoder
Why? Why are you creating all that extra work? Rule #1: leave generated code alone.
EJP