简体   繁体   中英

java call constructor without parameters from constructor with parameters

I have the next question. I have the code:

public class Foo {
  private int x;

  public Foo() {
    this(1);
  }

  public Foo(int x) {
    this.x = x;
  }
}

Can I call from Foo(int x) Foo() somehow?

您不能-这样做(调用this() )会在构造函数之间创建循环依赖关系,而这在编译时是不允许的

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM