• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Lazy Initialization

 
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is Lazy Initialization regarding variable initialization?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lazy Initialization : initialize(assign) a value to a variable when the program needs it.

for example:


 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 76392
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mals Raj, welcome to JavaRanch
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic