Considering that simple java code which would not work:
public class Bar extends AbstractBar{
private final Foo foo = new Foo(bar);
public Bar(){
super(foo);
}
}
I need to create an object before the super() call because I need to push it in the base class.
I don’t want to use an initialization block and I don’t want to do something like:
super(new Foo(bar)) in my constructor..
How can I send data to a base class before the super call ?
If
Foohas to be stored in a field, you can do this:Otherwise
super(new Foo(bar))looks pretty legal for me, you can wrapnew Foo(bar)into astaticmethod if you want.Also note that field initializers (as in your example) and initializer blocks won’t help either, because they run after the superclass constructor. If field is declared as
finalyour example won’t compile, otherwise you’ll getnullin superclass constructor.