versions : SonarQube 7.5
When I use sonarQube to analyze my code, a rule in sonarQube show “Remove the ‘volatile’ keyword from this field.”
But I want to create a singleton object. My code is like the following:
public class Singleton {
private static volatile Singleton singleton = null;
private Singleton(){}
public static Singleton getSingleton(){
synchronized (Singleton.class){
if(singleton == null){
singleton = new Singleton();
}
}
return singleton;
}
}
I think the keyword volatile is necessary. If remove volatile, it may catch an error.
So, I want to know whether this rule is correct.