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?
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.
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 ]
posted 12 years ago
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