Java: calling a constructor through a constructor, what's the point?

I see that some people do things like in the second constructor:

public class Apples {

  String color;
  int quantity;

  public Apples(String color, int quantity) {
    this.color = color;
    this.quantity = quantity;
  }

  public Apples(String color) {
    this(color, 0);
  }

}

What are the reasons for this? It seems to me that you are calling an additional method (constructor) just to save a few lines. I recall the professor a few years ago, saying that this is bad practice, but I don’t remember his reasons for talking about it.

+4
source share
7 answers

Computers today are so fast that calling additional methods for the convenience of reading and removing code redundancy is much more valuable than complex and duplicate code that no one can understand.

- , . , . - .

, 10 , . , / .

+10

DRY = . , . , . .

WET = ;)

+7

, , ,

public class Apples {

  String color;
  int quantity;

  public Apples(String color, int quantity) {
    this.color = color;
    this.quantity = quantity;
  }

  public Apples(String color) {
    this.color = color;  // code duplication
    this.quantity = 0;
  }

}
+4

, , , , . , , ( + ) init, .

, , . init, , ,

this.color = color; [- ] = ;

this.color = color; this.quantity = ;

, , , . , , 10+ , .

+2

API, ( ), , , :

public Apples(String color, int quantity) {
  this.color = color;
  this.quantity = quantity;
}

, , a default value :

public Apples(String color) {
  this(color, 0);
}

0, ( , ):

public Apples(String color) {
  this.color = color;
  this.quantity = 0;
}

, , , , , , ( exapmles); , constructors, .

BR.

+2

delegate call another constructor, "this (params)" .

      ,   int .

call prg ctrl , , , .

class Myclass1
{
  int a,b;

  Myclass1()
  {
    this(10,20);//delegate to Myclass(int,int)
    System.out.println("Myclass1() got job done from Myclass1(int, int)");

  }

  Myclass1(int q, int w)
  {
    //this();//err recursive constructor invocation
    System.out.println("Myclass1(int,int)");
    a =  q;
    b = w;   
  }

  void f()
  {
    System.out.println("in f()");
    f(10);//delegate call to f(10)
    System.out.println("done f()");
  }

  void f(int x)
  {
    System.out.println("x is  " + x);     
  }

  void disp()
  {
     System.out.println(a + "  " + b);
  }

  public static void main(String args[])
  {
    Myclass1 m1 = new Myclass1();
    Myclass1 m2 = new Myclass1(1,2);
    m1.disp();
    m2.disp();
   System.out.println("-------FN CALL DELEGATION---------------");
   m1.f();
   m1.f(99);
  }
}
+1

DRY, @Peter Lawrey , , . ? - . factory . Java , :

One of the advantages of static factory methods is that, unlike constructors, they have names.

Thus, using static factory methods, your code will look like this:

public class Apples {

  String color;
  int quantity;

  private Apples(String color, int quantity) {
    this.color = color;
    this.quantity = quantity;
  }

  public static Apples newInstance(String color, int quantity) {
    return new Apples(color, quantity);
  }

  public static Apples newEmptyInsatnce(String color) {
    return new Apples(color, 0);
  }

}

Thus, the Apples.newEmptyInstance () method is more informative about what it creates than just an overloaded constructor. I just assumed why your professor said that it was a bad idea.

+1
source

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


All Articles