dev@glassfish.java.net

Re: Findbugs exception

From: Bobby Bissett <bbissett_at_gmail.com>
Date: Fri, 19 Apr 2013 08:08:40 -0400

On Fri, Apr 19, 2013 at 6:42 AM, John Wells <john.wells_at_oracle.com> wrote:

> [...]
> This is the only thing in the file that ever sets the controller variable,
> and the variable never becomes unset. To me this is valid java...
>

It's "valid" I guess, but it won't work when you want it to. The usual
answer to this question is:

http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html

(Very short summary: non-null doesn't mean initialized correctly.)

If you really want to use lazy initialization, and you really really don't
want to synchronize on the method, try the lazy initialization holder class
method instead. Lots of examples online.

Cheers,
Bobby