Path: csiph.com!weretis.net!feeder4.news.weretis.net!news.szaf.org!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: How to assign an object to another in it method? Date: Sun, 18 Jun 2017 15:31:59 +0200 Lines: 76 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net E4e05TaOG3tbqtq81KgHWgBIwYZx+ScE8adosczoQKTs/PBDI= Cancel-Lock: sha1:7ySHY/Lms3lVWZKe1EpAk4WL7KA= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1 In-Reply-To: Content-Language: en-US Xref: csiph.com comp.lang.java.programmer:36671 On 18.06.2017 14:02, parmenides wrote: > 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(); If I understand correctly you have an instance method that reads state of an object from a database. Then you want to assign that state to another instance. But what for? You could just keep it in the object that it was read into. > That is I want to read data from Database, and instanciate an object > from these data. This is not what the code shows. The object is instantiated with a default constructor and - we have to assume - readFromDB() updates state of that object, presumably from a DB query result. > Then, the object is assigned to another object. In You can only assign object references to variables. What you seem to mean here is that you want to copy state from one instance to another. Note that depending on what Foo's state contains you would need to make sure you do not introduce errors via aliasing. > 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. I think your design could be better. You just should make someMethod() static and have it return the new instance. Copying around does not make sense since f1 is lost after the method call anyway so the copying is just wasting CPU cycles. So, just do: class Foo { public static Foo newFromDB() { final Foo f = new Foo(); f.readFromDB(); return f; } } Foo f2 = Foo.newFromDB(); Btw. where does the DB context come from? Apparently you are transporting it via some non obvious way (global variable, static member, thread local). This is quite non obvious and may impose some obstacles to testing. Also, there could be multithreading issues in some of these cases. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/