| Author |
Lazy Initialization
|
sameera liyanage
Ranch Hand
Joined: Nov 25, 2008
Posts: 690
|
|
|
what is Lazy Initialization regarding variable initialization?
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Lazy Initialization : initialize(assign) a value to a variable when the program needs it.
for example:
|
 |
Mals Raj
Greenhorn
Joined: Mar 09, 2009
Posts: 4
|
|
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
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
Mals Raj, welcome to JavaRanch
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
|
Should not be used unless it's proovable going to save time. This is when the initialization includes some heavy time consuming processes.
|
JDBCSupport - An easy to use, light-weight JDBC framework -
|
 |
Mals Raj
Greenhorn
Joined: Mar 09, 2009
Posts: 4
|
|
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.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
R van Vliet
Ranch Hand
Joined: Nov 10, 2007
Posts: 144
|
|
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.
|
 |
R van Vliet
Ranch Hand
Joined: Nov 10, 2007
Posts: 144
|
|
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
|
 |
Muhammad Khojaye
Ranch Hand
Joined: Apr 12, 2009
Posts: 341
|
|
|
Lazy Initialization has also been discussed in Effective Java where some alternatives like double-check idiom and single-check idiom methods been advised too.
|
http://muhammadkhojaye.blogspot.com/
|
 |
 |
|
|
subject: Lazy Initialization
|
|
|