Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lazy Initialization

 
sam liya
Ranch Hand
Posts: 1240
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is Lazy Initialization regarding variable initialization?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lazy Initialization : initialize(assign) a value to a variable when the program needs it.

for example:


 
Mals Raj
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mals Raj, welcome to JavaRanch
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should not be used unless it's proovable going to save time. This is when the initialization includes some heavy time consuming processes.
 
Mals Raj
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
R van Vliet
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lazy Initialization has also been discussed in Effective Java where some alternatives like double-check idiom and single-check idiom methods been advised too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic