Campbell Ritchie wrote:Do you mean pass them as method arguments or as constructor arguments?
Yes somewhat like that but not as method arguments or constructor arguments. I should be able to access reference of the object created in multiple classes but initialization will be done once.
Campbell Ritchie wrote:Do you mean pass them as method arguments or as constructor arguments?
Yes somewhat like that but not as method arguments or constructor arguments. I should be able to access reference of the object created in multiple classes but initialization will be done once.
Then I don't understand why a constructor argument wouldn't work.
@OP: It's really hard to give examples without knowing how you're going to use the object, but here's the shell of a class that you could use to get a database connection object that must be initialized only once:
You would use the class like this: Connection con = DbConnection.getConnection(); The first time you call this it will go through the initialization of the con object. Subsequent times it would just return the initialized object.
All things are lawful, but not all things are profitable.
Swapnil Sanghai wrote:@ Paul Clapham - Apart from this I wanted to know other way to handle this situation.
Okay, you could also use a setter method in the User class and pass the Target reference in that way. I will leave you to modify my example to do that.
Swapnil Sanghai wrote:@ Paul Clapham - Apart from this I wanted to know other way to handle this situation.
Okay, you could also use a setter method in the User class and pass the Target reference in that way. I will leave you to modify my example to do that.