Create an object before the super call in Java
Consider the simple java code that doesn't 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 parent class
I don't want to use initialization blocks. I don't want to do similar things:
Super (New foo (bar)) is in my constructor
How to send data to the mother shift before the super phone?
Solution
If you must store foo in a field, you can do the following:
public class Bar extends AbstractBar{ private final Foo foo; private Bar(Foo foo) { super(foo); this.foo = foo; } public Bar(){ this(new Foo(bar)); } }
Otherwise, super (New foo (bar)) seems legal to me. If you like, you can wrap the new foo (bar) into a static method
Also note that field initializers (as shown in the example) and initialization blocks are useless because they run after the superclass constructor If the field is declared final, your example will not compile, otherwise you will get null in the superclass constructor