How to copy the state of a JavaBean to another existing object?

You have a class that looks like this:

class Foo { public String x; public int y; public long l; } 

then somewhere we have:

 Foo foo1 = new Foo(); foo1.x = "a"; foo1.y = 3; foo1.l = 2L; 

and then in another place we have another Foo object whose fields have not been initialized.

 Foo foo2 = new Foo(); //foo2 = foo1; 

Given that assigning a reference to an object represented by foo1 to a variable foo2 is not what I want to do here, since I want to keep a reference to an object represented by foo2, what is the easiest way to copy the state of foo1 to foo2?

In other words, how to do this automatically?

 foo2.x = foo1.x; foo2.y = foo1.y; foo2.l = foo1.l; 
+4
source share
6 answers

If Foo was a JavaBean, that is, if it had the correct setters / receivers instead of public fields, you could use Apache Commons Beanutils.copyProperties(Object dest, Object orig)

+3
source

There is no MOVE CORRESPONDING in Java. You will need to write it down completely (or "some other hack").

The code is not idiomatic for Java. There are public fields. Probably the best way to deal with a situation that is not described is to have immutable Foo . Then you can have an object with a link to Foo that can change.

+2
source

create copy constructor

 class Foo { public String x; public int y; public long l; public Foo(Foo foo){ this.x = foo.x; this.y = foo.y; this.l = foo.l; } } 

Now you can use this as

 Foo foo1 = new Foo(); foo1.x = "a"; foo1.y = 3; foo1.l = 2L; Foo foo2 = new Foo(foo1); 

You can also do this by following the clone method. But I think creating a copy constructor is better and easier. check this link efficient java

+1
source

You can either implement the copy constructor, for example

 public Foo (Foo foo) { this.x = foo.getX(); this.y = foo.getY(); this.z = foo.getZ(); } 

and then create a new copy of Foo as

 Foo foo2 = new Foo(foo1); 


Or you can use foo to clone and define clone() as

 public Foo clone() { return new Foo(this.x, this.y, this.z); } 

and then create a copy using clone() as

 Foo foo2 = foo1.clone(); 

Please note that you can skip the definition of clone() (here), because by default the implementation of Object.clone() will execute a shallow copy and will work with the simple fields that you have.

0
source

Besides the Cloneable and copy interface constructors, there is another solution: a freeze / thaw pattern .

This is a template that allows you to get a builder from an object, with all the conditions of this object pre-filled, if necessary, change it and freeze it again. If you implement it, you can do:

 final Foo foo2 = foo.thaw().freeze(); 

The bonus point is that all instances of Foo immutable: there are no more setters! In order to change:

 final Foo foo2 = foo.thaw().setY(something).freeze(); 

These are JavaBeans without any flaws and the ability of the linker template to load.

0
source

You could implement the Foo Cloneable interface and override the clone method. In the body of the method, you initialize a new Foo and assign this value to the Foo field to the new Foo.

0
source

Source: https://habr.com/ru/post/1485639/


All Articles