I am well aware that it is not good practice to synchronize on Boolean. There is a lot explanation why it's not good such as:
Why is it not a good practice to synchronize on Boolean?
etc.
So, it is clear that this is BAD practice:
The code synchronizes on a boxed primitive constant, such as an Boolean.
private static Boolean inited = Boolean.FALSE;
...
synchronized(inited)
{
if (!inited)
{
init();
inited = Boolean.TRUE;
}
}
...
What interests me is what happens if we create final static Boolean with "new" operator, i.e. someting like this (this is from real code I haven't wrote but I maintain it, names of methods etc. are changed):
private final static Boolean lock = new Boolean(true);
...
public static SomethingGeneric getSomething()
{
synchronized(lock)
{
if (somethingElse == null)
{
try
{
somethingElse = persistence.valueobject.getSomeValue(GET_THAT);
System.out.println("blah blah");
}
catch (ObjectCreationException oce)
{
// report the error
log.error("There was this and that error", oce);
System.out.println("Could not create it");
}
}
return somethingElse;
}
}
Would it be then "legal" to use it? Same as if we used Object such as in:
private final Object lock = new Object();
or
private static final Object lock = new Object();
public void doSomething() {
synchronized (lock) {
// ...
}
}