Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Object creation in loop  RSS feed

 
Saurabh Joshi
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am in kind of argument with one of my friend on the need for object creation in loop.

Can you please let me know in java will there be any situation when I will need object creation in loop.

Please help me out for this.

Thanking you in anticipation.

Saurabh.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saurabh Joshi wrote:
Can you please let me know in java will there be any situation when I will need object creation in loop.

Many time, you ll come across such need, I, as a web developer, when add some beans(DTO, VO) in list, then I have to create an object for each bean every time a new row is fetched, like

 
Saurabh Joshi
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Sagar,

Thanks for that quick response.
Ya I agree to your point but besides this do have any other situation where we require to have objects in loop as per the performance criteria in mind.

Saurabh.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saurabh Joshi wrote: situation where we require to have objects in loop as per the performance criteria in mind.

Look, performance, optimization this things you should not worry unless there is really such an issue comes, as speed, memory issues, etc. What I recommend is write a *good* code, If that code required an creation of object in a loop, then don't worry. Trying optimization of code from performance point of view in development phase, dosen't yield anything[and its isn't mean to avoid it totally]. Later if you came to know that its wrong what you were doing by crating an object in loop, you would be smart and experienced enough, that you can have other good solution on that problem.

So basically, If you need to create an objects in loop and you don't think of any other way to do it, then, I recommend, do it. Later by profiling, we can check out that, is your loop causing a problem to application by creating so many objects.
 
Saurabh Joshi
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya that is right too much of optimization makes a mess. Anyways I will make a note of your suggestions.
Thanks Sagar.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saurabh Joshi wrote:
Thanks Sagar.

You're Welcome, But many other fellow Ranchers will put more light on this, for us
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really dont understand the objection to "Object creation in loop" - if you need the object, create it and use it. The exact location of the declaration of the reference variable should depend on program logic, not some false sense of efficiency.

Strive for clarity of expression first - if somebody looking at your code says "what the is this for?" you need more clarity.

For fun, do a google search for "premature optimization is the root of all evil"

Bill
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!