ovod
ovod

Reputation: 1178

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?

Upvotes: 0

Views: 84

Answers (1)

Reimeus
Reimeus

Reputation: 159874

You can't - doing so (calling this()) would creating a cyclic dependency between the constructors which isnt allowed at compile time

Upvotes: 2

Related Questions