While trying to understand the Dependency Injection principle I came across this example which I couldn't understand
abstract class ExternalInvestmentBase {
private static ExternalInvestmentBase sImpl;
protected ExternalInvestmentBase() {
sImpl = this;
}
public static String supply(String request) throws Exception {
return sImpl.supplyImpl(request);
}
abstract String supplyImpl(String request)
throws Exception;
}
class InvestmentUtil extends ExternalInvestmentBase {
public static void init() {
new InvestmentUtil();
}
@Override
public String supplyImpl(String request) throws Exception {
return "This is possible";
}
}
public class IExternalInvestment {
public static void main(String[] args) throws Exception {
InvestmentUtil.init();
String rv = ExternalInvestmentBase.supply("tt");
System.out.println(rv);
}
}
The main question is
- How does the "this" keyword in the base class work ?
- How did the
ExternalInvestmentBase.supply("tt");
access the object ?