Here is a similar code segment making use of Lazy Instantiation. The code in Listing 3 performs the same function as in Listing 2 except that the object1 variable is being managed via Lazy Instantiation. When MyClass is created, nothing is done with object1 other than implicitly setting its reference to null. If it does not exist, an instance is created and then used. When it comes to speed, the static version wins.
Static methods are quicker than normal ones, as there is no code execution overhead.
Classes implementing Lazy Instantiation contain management code that needs to be executed whenever access to a variable is required. In Listing 3, overhead is incurred every time object 1 is accessed, because it first needs to be checked for a null value. For ease of programming, static wins again. Only a single line of code is required to implement a variable with a static initializer. As mentioned above, Lazy Instantiation requires that an object be checked for a null value every time it is accessed.