Using "this" in another constructor

Suppose you have a class with this constructor:

public SomeObj(int x, int y) {
    this.x = x;
    this.y = y;
}

      

Things are good. But now, if you want to clone an object, I want the constructor to take one argument with an object of that type, so all (required) fields can be copied inside the constructor.

public SomeObj(SomeObj objectToClone) { ... }

      

But which of the following two methods is better? What are the advantages and disadvantages (performance (bytecode), readability ...)?

// 1
public SomeObj(SomeObj objectToClone) {
    this.x = objectToClone.x;
    this.y = objectToClone.y;
}

// 2
public SomeObj(SomeObj objectToClone) {
    this(objectToClone.x, objectToClone.y);
}

      

+3


source to share


6 answers


I would go with him personally.



Whenever possible, I try to make exactly one constructor that has a "real" body and force everyone else to delegate to it. This is not always possible - in particular, different constructors may need to delegate to different superconstructors, but it's nice to know that additional initialization, logging, breakpoints, etc. can be placed there that will always hit.

+13


source


Number 2 is better. What for? because you don't repeat yourself anymore setting the member in two different constructors.



There is really no performance, unless the slight additional indirection of the call this()

affects you (and I doubt you can accurately measure this difference accurately).

+3


source


If you use the second option, you don't need to change public SomeObj(SomeObj objectToClone)

every time you change the implementation public SomeObj(int x, int y)

. So it's better because it avoids duplication of code.

+2


source


The latter is no longer created as soon as you add another parameter, say z

. If you take the first approach, you might just forget to copy z

. It still builds but doesn't work as expected.

+1


source


This is not C ++. Override .clone () method from Object class for copy objects. It's true

+1


source


// 1
public SomeObj(SomeObj objectToClone) {
    this.x = objectToClone.x;
    this.y = objectToClone.y;
}

      

it's better.

The second option will require you to update your constructor every time you add a new parameter. Also, if you have to call it in 100 places with 100 properties for a list in each constructor, it's obvious that it starts to lose readability.

I can't figure out why you want to pass multiple properties of the same object as a parameter instead of passing the object. There is no reason for this.

Compare these two:

public SomeObj(SomeObj objectToClone) {
    this.x = objectToClone.x;
    this.y = objectToClone.y;
    this.z = objectToClone.z;
    this.p = objectToClone.p;
    this.s = objectToClone.s;
    this.a = objectToClone.a;
    this.b = objectToClone.b;
    }

      

and then run SomeObj(obj)

10 times in your code.

versus SomeObj(obj.x,obj.y,obj.z,obj.p,obj.s,obj.t,obj.a,obj.b);

10 times in your code

Also, with the second option, you are not guaranteeing that the passed parameters are what you want, so you can get something like this: SomeObj(obj.x,obj.y,obj2.z,obj3.p,0,0,-1,null);

0


source







All Articles