I want to wrap a class in Java, but the problem is this:
public class A {
public A() {
doSomething();
}
public void doSomething() {
}
}
Now when I try to wrap this class and delegate all methods to the shell
public class Wrapper extends A {
private final A a;
public Wrapper(A a) {
super();
this.a = a;
}
@Override
public void doSomething() {
this.a.doSomeThing();
}
}
of course, I get NPE because "a" is still null because it is set after a super () call that calls the overriden doSomething () method. Is there a solution to this problem? The only thing that occurred to me was to make a factory method and set a static variable containing a reference to a, but that seems ugly to me.
source
share