File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Lazy Initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Lazy Initialization" Watch "Lazy Initialization" New topic
Author

Lazy Initialization

sam liya
Ranch Hand

Joined: Nov 25, 2008
Posts: 1109
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: 38765
    
  23
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: 19693
    
  20

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 - OCEEJBD 6
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: 449

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/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lazy Initialization