Looking through some Java code the other day, I saw something I haven't seen before and am slightly confused on. Part of the code is shown below:
public class PreferredAlternateProxy {
...
private static PreferredAlternateProxy instance;
...
public static PreferredAlternateProxy getInstance(){
if(instance == null)
createInstance();
return instance;
}
private static synchronized void createInstance(){
if(instance == null)
instance = new PreferredAlternateProxy();
}
}
What is the purpose of declaring a static property of of the same class as the public class? In what situation is this useful?
What is the purpose of making the second method (createInstance()) synchronized?