Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Initializing an object for reuse

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a question regarding reusing an object. I jave a simple pojo with 7 different String and Integer instance variables.

I am doing some manipulation where I populate this pojo and then add it to a list. At this point I can reuse this pojo by initializing all its fields, instead of jsut creating a new pojo. But I am confused as to how I can invoke the constructor of this pojo (where I am initializing all the instance variables) without usign teh new keyword which would actually create a new object?


The above initialization yields a compiler error? Am i doing this right? it seems simple enough?

Any help appreciated. Thanks!

Regards
 
Marshal
Posts: 67463
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a beginners' question.

You can't invoke the constructor of an object, only create an object from a class by calling a constructor. You might be able to use set methods to alter all those fields, but what you are doing doesn't look like good object-oriented design to me.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't call a constructor. If you want to reinitialise your object you need to write a method to do the initialisation. You can then call this method both from your constructor and from the code when you need to reinitialise.

You do realise that your list only contains a reference to the object don't you ? So if you make any changes to the object, the object referred to by the list will also change (because they are one and the same thing).
[ January 16, 2008: Message edited by: Joanne Neal ]
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies, you are right. I see that all the items I added to my list are the same object. So I guess reusing the object will not work.... I need to create a new object everytime.

Thanks guys, once again great help.
 
What are you doing in my house? Get 'em tiny ad!
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!