No enclosing instance of type SearchCommand is available due to some intermediate constructor invoca

public class SearchCommand extends Command {
    
    private static final String ACTION = "search";
    public SearchCommand() {
        this(ACTION, new SearchCommandHandler());// error: No enclosing instance of type SearchCommand is available due to some intermediate constructor invocation
    }

    public SearchCommand(String action, CommandHandler handler) {
        super(action, handler);
    }
    
    class SearchCommandHandler implements CommandHandler {
        @Override
        public void execute(PrintStream out, Properties args) {
            out.println(args.toString());
        }
        
    }

}


原因:

http://stackoverflow.com/questions/2741066/why-does-a-sub-class-class-of-a-class-have-to-be-static-in-order-to-initialize-t


Basically an inner class (without the static modifier) has an implicit reference to an instance of its outer class, so it can't be created until the outer class is created. By creating one on the call to this it can't reference the outer class yet because the outer class isn't constructed much at all until after the call to super. The case that works for you, the assignment to head happens after the (implicit) call to super so the class is constructed enough to get a reference to it.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值