Forums Register Login

Lazy Initialization

+Pie Number of slices to send: Send
what is Lazy Initialization regarding variable initialization?
+Pie Number of slices to send: Send
Lazy Initialization : initialize(assign) a value to a variable when the program needs it.

for example:


+Pie Number of slices to send: Send
Yes, the idea is to delay the creation of an object as this creation could be expensive depending on the circumstances. It could be I guess assigning a new value to a variable, populating a list from a db call, initialising a new object etc.

below is a very basic example

+Pie Number of slices to send: Send
Mals Raj, welcome to JavaRanch
+Pie Number of slices to send: Send
Should not be used unless it's proovable going to save time. This is when the initialization includes some heavy time consuming processes.
+Pie Number of slices to send: Send
Thanks Ritchie!

And agree with the last comments also partially, the reason to use lazy initilialisation must be for a good reason and lazy loading for that matter.
+Pie Number of slices to send: Send
 

Sebastian Janisch wrote:Should not be used unless it's proovable going to save time. This is when the initialization includes some heavy time consuming processes.


I wouldn't be as strict as to say "should not be used", but I do agree that using lazy evaluation just because you can is a form of premature optimization which adds nothing but added complexity. I agree that for creation of a very few simple objects it's just not worth it.
+Pie Number of slices to send: Send
 

Rob Prime wrote:

Sebastian Janisch wrote:Should not be used unless it's proovable going to save time. This is when the initialization includes some heavy time consuming processes.


I wouldn't be as strict as to say "should not be used", but I do agree that using lazy evaluation just because you can is a form of premature optimization which adds nothing but added complexity. I agree that for creation of a very few simple objects it's just not worth it.



Actually I think "should not be used" is fairly accurate. There's really one and only one reason to do lazy initialization and that's when initializing the resource causes a noticable performance bottleneck. In all other situations it should be avoided since it introduces code complexity that may make your life and that of the person that ends up maintaining the code harder. It's purely a performance optimalization and I don't think it should be used in any situation where such an optimization is not necessary.
+Pie Number of slices to send: Send
 

Mals Raj wrote:Yes, the idea is to delay the creation of an object as this creation could be expensive depending on the circumstances. It could be I guess assigning a new value to a variable, populating a list from a db call, initialising a new object etc.

below is a very basic example



If you'll allow me to be slightly anal that should probably be :



Which would allow other bits of code that do not necessarily require the list to be populated to use the collection instance (i.e. code like listOfObjects.contains(mySpecificItem) would not require a null check).

:p
+Pie Number of slices to send: Send
Lazy Initialization has also been discussed in Effective Java where some alternatives like double-check idiom and single-check idiom methods been advised too.
This tiny ad is suggesting that maybe she should go play in traffic.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1942 times.
Similar Threads
Doubt in Effective Java
what is lazy initialization in hibernate
General
org.hibernate.LazyInitializationException
lazy initialization
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:05:36.