Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the differance ?

 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have a basic questions in the following code snippent what is the best way of declaring the mp object..which will take less memory..
regards,
Arun


[ April 26, 2002: Message edited by: Dirk Schreckmann ]
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They will take the same memory. The only difference is when the object is created, during the construction of the class, or when the method is called, the latter being a case of delayed initialization.
--Mark
 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for you reply...
Which one you prefer?
Arun
[ April 27, 2002: Message edited by: arun mahajan ]
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for delaying initialization is to allow the delaying the creation of memory intensive objects until the last minute. If this fellow is going to be HUGE then you might want to delay - and then null out that variable as soon as possible.
Otherwise it is probably better to create the object at the place the variable is declared. If the initialization is too far removed from the variable declaration you run the risk that the reader will have forgotten the "type" of the variable by the time you get around to putting something in it.
Saving programmer time is a big issue also.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic