You need to implement it as follows:
public class Foo {
public Foo() {
this(makeBar());
}
public Foo(Bar b) {
}
private static Bar makeBar() {
Bar b = new Bar();
b.setSomeData();
b.doSomethingElse();
return b;
}
}
The method makeBarmust be static, because the object corresponding to thisit is not available at the point that you call the method.
By the way, this approach has the advantage that it passes a fully initialized object Barto Foo(Bar). (@RonU notes that his approach does not do this. This, of course, means that his constructor Foo(Bar)cannot assume that his argument Foois in the final state. This may be problematic.)
, , factory .