Initiating objects on demand with the lazy initialization pattern
This pattern’s purpose is to defer an instance of the desired class instance until the client actually requests it.
Motivation
Although operational memory has grown drastically over the years, we learned in the previous chapter that the JVM allocated a defined, specific size of memory reserved for the heap. When the heap is exhausted and the JVM is unable to allocate any new object, it causes an out of memory error. Lazy handling can have quite a positive impact on this heap pollution. It is sometimes also called asynchronous loading because of the delayed instance. The pattern has quite a nice use in a web application where the web page is generated on demand rather than during the application initialization process. It also has its place in an application, where the cost of operating the relevant object is high.
Finding it in the JDK
Lazy initialization can be demonstrated using the example of dynamic...