Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: v_borchert@despammed.com (Volker Borchert) Newsgroups: comp.lang.java.programmer Subject: Re: How to assign an object to another in it method? Date: 19 Jun 2017 17:13:49 GMT Organization: Private site at Eddersheim, Germany Lines: 42 Distribution: world Message-ID: References: X-Trace: individual.net Wgkx5y8QZ4Dh/AUhM7DSmQH56GiIPPHxmKyVOHycEw/kLAjsjR Cancel-Lock: sha1:e9aN97CcABZydrCrUpS3FyFaGtM= Xref: csiph.com comp.lang.java.programmer:36680 parmenides wrote: > Hi, > > Suppose that there is a class 'Foo' and an its instance called 'f1'. > I want to assign 'f1' to another instance called 'f2' within a method > invoked by f2. The code like this: > > Class Foo { > ... ... > public someMethod() { > Foo f1 = new Foo(); > f1.readFromDB(); // Now I get an instance > this.XXX = f1.getXXX(); > this.YYY = f1.getYYY(); > ... ... > } > } > ... ... > Foo f2 = new Foo(); > f2.someMethod(); > > That is I want to read data from Database, and instanciate an object > from these data. Then, the object is assigned to another object. In > someMethod(), the feasible way is to assign the member variables one by > one. But, I think this is cumbersome at all. So I try to do the > assignment as a whole like this: > this = f1; > and write a clone() method to support this kind of assignment. However, > owing to 'this' is just an reference to 'f2' rather than an object, the > compiler does not agree. Is there any way to do assigment as a whole in > this situation? Thanks. Why do you want to do that? You will then have two distinct object representing the same database content. What if one of them is changed afterwards? Which of them will be authoritative? -- "I'm a doctor, not a mechanic." Dr Leonard McCoy "I'm a mechanic, not a doctor." Volker Borchert