Hey So I was examining the following code I found an online( https://javarevealed.wordpress.com/tag/initialization-on-demand-holder ) , which implements the Singelton design pattern using double checked locking like so:
public static volatile SingletonExample getSingletonInstance() {
if (null == singletonInstance) {
synchronized (SingletonExample.class){
if (null == singletonInstance) {
singletonInstance = new SingletonExample();
}
}
}
return singletonInstance;
}
My question here is what is the point of having the volatile keyword in the method signature here? I thought the volatile modifier could be only be used on fields?
Any help would be great.
Thanks.