java 

Send to Kindle
home » snippets » java


Pages
di        
generics        
jar        
javadoc        
libraries        
reflection        
testing        



HowTo

Constructor Chaining

From: How do I call one constructor from another in Java?

The best way is from the smallest constructor to the largest.

public class Cons {  
  public Cons() {  
    this(madeUpArg1Value,madeUpArg2Value,madeUpArg3Value);  
  }  
  public Cons(int arg1, int arg2) {  
    this(arg1,arg2, madeUpArg3Value);  
  }

  public Cons(int arg1, int arg2, int arg3) {  
    // Largest constructor that does the work  
    this.arg1 = arg1;  
    this.arg2 = arg2;  
    this.arg3 = arg3;  
  }  
}