Java Musings – initializing a final variable with an uninitialized final variable

Java has fewer quirks compared to C++, but sometimes I do come across surprises.
A code like following will fail to compile since you are trying to initialize a variable with an uninitialized variable.

But if instead of directly referencing mField1, you reference indirectly via a getter method code will compile, and mField2 will get “null” value for mField1.

One Reply to “Java Musings – initializing a final variable with an uninitialized final variable”

  1. Good catch!

Leave a Reply

Your email address will not be published. Required fields are marked *