Interview Question
Software Engineer in TestsStatic is a class variable it is intialized only once...ordinary variables are object specific variables..each object will have its own copy of variables but it is not the case with Static variables..the same static variable is accessed by all the objects of the class(and so called class variable)...and the reason why we use static object variable in Singleton class so that whenever you try create object and since it is a static variable and holds the object reference we check it is NULL if not NULL we return the same reference by avoiding creation of multiple objects
Static is a class variable it is intialized only once...ordinary variables are object specific variables..each object will have its own copy of variables but it is not the case with Static variables..the same static variable is accessed by all the objects of the class(and so called class variable)...and the reason why we use static object variable in Singleton class so that whenever you try create object and since it is a static variable and holds the object reference we check it is NULL if not NULL we return the same reference by avoiding creation of multiple objects
Static is a class variable it is intialized only once...ordinary variables are object specific variables..each object will have its own copy of variables but it is not the case with Static variables..the same static variable is accessed by all the objects of the class(and so called class variable)...and the reason why we use static object variable in Singleton class so that whenever you try create object and since it is a static variable and holds the object reference we check it is NULL if not NULL we return the same reference by avoiding creation of multiple objects
Static class Vs Singleton Pattern ?
- BrainLess January 31, 2012stackoverflow+difference-between-static-class-and-singleton-pattern