java - Throw Exception then Call Constructor? -


so i'm building test library use personal use have question.

with java, if have 2 or more constructors in class, if wish call 1 another, must first thing do. problematic me have below setup.

public constructor(typea a, typeb b, typec c) {     if (c.getdata() == null) throw new illegalargumentexception("");     this(a, b, c.getotherdata()); }  public constructor(typea a, typeb b, typed d) {     // stuff happens } 

how can this, avoiding the, "constructor call must first statement in constructor" error?

you cannot want constructors. instead use static factor method this:

public static typething buildmything(typea a, typeb b, typec c) {     if (c.getdata() == null) throw new illegalargumentexception("");     return new typething(a, b, c.getotherdata()); }  public constructor(typea a, typeb b, typed d) {     // stuff happens } 

Comments