Sunday 17 May 2015

What happens If Singleton object is not STATIC ?

Look at the below code 

   private static AnalyticsLogRotator intance = null;

    public static synchronized AnalyticsLogRotator geInstance() {
        if(intance == null) {
            intance = new AnalyticsLogRotator();
        }
        return intance;
    }


If instance, is not STATIC, you will not be able to refer it from static method geInstance().
If getIstance() is not static method, you will not be able to invoke it without creating an object of this class.
And if can create an object of this class on your own, then it will not be singleton :) 

No comments:

Post a Comment