Timo Nentwig said:
public class Blah
{
private static Blah singleton;
public static Blah getInstance()
{
if (singleton == null)
singleton = new Logfile();
return singleton;
}
}
Why can't I declare singleton as static final?
A final variable must either be initialised at the point of declaration, or
at the very least the compiler must be convinced they will be initialised
before there is any possibility they may be used.
so with static either:
class Mine {
static final Object obj1 = new Object();
// or
static final Object obj2;
{obj2 = new Object();}
// or
final Object obj3;
Mine() {
obj3 = new Object();
}
}
since in all cases the Objects must be initialised before they are used.
You cannot initialise a final attribute in a method, since other methods
that use that variable maybe called beforehand.
Therefore you cannot have the lasy behaviour you are after with a final
attribute.
You must do:
private static final Blah singleton = new Logfile();