Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

lazy initialization

 
basia fish
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

how do i use lazy initialization
how would it change code like this

private int array[];

public void setArray(int arr[])
{
array=arr;
}
public int[] getArray)()
{
return array;
}

help really apprecaited
thanks a lot
barb
 
Dipen Javia
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey barb,

I am sorry barb but i couldnt understand what you are trying to do with that code and I didnt understand the term "lazy initialization".
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lazy instantiation/initialisation means you don't initialise a member until the first time that member is requested somewhere.

Typically you'd have something like


Typically it's used when the initialisation of the data is expensive and won't be required for every instance of the class. Using lazy instantiation in such cases yields a performance win for many users of the class either by using less CPU cycles and/or less memory.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic