More than one instance (per JVM) could (probably accidently) be created, if some race condition occur.
This code actually reflect a "lazy singleton pattern", i.e. the purpose would be to have only one instance per JVM, created at the moment when it is first accessed.
=> at first glance we could only have once instance of it. However, since the code is not synchronized, it could happen that this part of the code is executed several times in parallel in a multi-thread environment.
if ( INSTANCE == null )
{
INSTANCE = new Gingleton();
}
See also comments here
Singleton pattern with combination of lazy loading and thread safety
singleton pattern in java. lazy initialization
So I would say this is bad question and you could bash the MC author in the head, because:
- More than one instance of Gingleton can be created (My choice)
=> True, given discussion above, but that is probably not be what the author of the question has in mind
- A Gingleton will never be created
=> errr, well, getInstance()
is never called in this snippet => nope, no instance is created. But probably the author has in mind that we could call this method, in which case the answer is yes
- The constructor is private and can't be called
A private constructor can be called... just not from outside the class. Again, the author is not formulating it but we could assume that he or she only thinks of the case "from outside"
- value can be garbage collected, and the call to getInstance may return garbage data
Ah, this statement at least is clear. No, the static field will not be garbage collected, you can rely on its value.