-2

Assume that the User class has a lot of properties that I want to copy to another instance in a copy constructor. Is there a shortcut way to avoid manually writing an assignment for each field?

For example:

public class User {
    private Integer field1;
    private Integer field2;
    private Integer field3;
    private Integer field4;
    private Integer field5;
    private Integer field6;
    private Integer field7;
    private Integer field8;
    private Integer field9;
    private Integer field10;
    private Integer field11;
    private Integer field12;
    private Integer field13;
    // and more others fields here

    public User(User other) {
        this.field1 = other.field1;
        // eleven fields here
        this.field13 = other.field13;
        // and more others fields here
    }
}
bmm6o
  • 6,187
  • 3
  • 28
  • 55
Rasool Ghafari
  • 4,128
  • 7
  • 44
  • 71
  • 2
    No, there is not. `this` is not a variable, it is a keyword. What do you want to achieve? – Thilo Jun 05 '19 at 11:06
  • How to make your colleagues curse your name: define a variable called `thıs`. – Andy Turner Jun 05 '19 at 11:21
  • You should put your properties in designated subclasses or containers, group what belongs together. Then you just need to "copy" a hand full of containers instead of dozens of fields. Having that many fields usually just indicates a bad design. – Zabuzard Jun 05 '19 at 12:52
  • No, but you can write a constructor that takes a User. You would then invoke the constructor you have with all the fields from the passed in user object. – Thorbjørn Ravn Andersen Jun 05 '19 at 15:11
  • You can use libraries like Lombok or Immutables to generate copy-constructors/builders. – Thilo Jun 05 '19 at 23:32
  • Any feedback on the answers? – GhostCat Jun 08 '19 at 10:17

2 Answers2

2

No, you can't.

You should use copy constructor.

Copy all variables from parameter 'user' to all variables of 'this'.

ProS
  • 21
  • 2
0

Here:

this = user;

is simply not valid. This here is though:

this.user = user;

You have to understand that you are in the body of a method that "belongs" to some object. Within the method body, you can use this to access this "owning" object. And you can then use this.someField to address a field of that owning object. Either for reading that field, or for writing to it.

But as said: that code runs within a method that "belongs" to an owning object. Therefore it is impossible (and also meaningless) to attempt to "change" that owner from within an "owned" method.

What you are probably looking for would be a copy constructor, see here for a simple example.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • You’ve seen it is another instance of the same class passed in to the constructor? – Thorbjørn Ravn Andersen Jun 05 '19 at 11:13
  • @ThorbjørnRavnAndersen And? It doesn't matter, `this` isn't something that can stand alone on the left hand side of an =, no matter what you have on the right hand side. So yes, he got the signature of a copy constructor, but obviously he didn't know how to proceed from there. – GhostCat Jun 05 '19 at 11:24