I've heard that when instantiating a Java object there are actually 2 code commands happening, the memory allocation and the actual constructor.
So when declaring a singleton we need to write it like this:
if (instance == null) {
synchronized (lock) {
if (instance == null) {
var tmp = new SingletonObject();
instance = tmp;
}
}
}
I wanted to know if this applies to try catch finally as well. or is this problem occur only in threads?
FileOutputSteam out = null;
try {
out = new FileOutputStream(path);
...
} finally {
if (out != null) { // Does out can be non null but be corrupt?
out.close();
}
}
// BTW: This is an example, I know that there is try-with-resource
Thanks, Ido Sorozon
finallyblock it must be a fully constructedoutobject