java抽象静态方法,我们可以在抽象类中使用静态方法吗?

In Java Programming, Can we call a static method of an abstract class?

Yes I know we can't use static with a method of an abstract class. but I want to know why.. ?

解决方案

In Java you can have a static method in an abstract class:

abstract class Foo {

static void bar() { }

}

This is allowed because that method can be called directly, even if you do not have an instance of the abstract class:

Foo.bar();

However, for the same reason, you can't declare a static method to be abstract. Normally, the compiler can guarantee that an abstract method will have a real implementation any time that it is called, because you can't create an instance of an abstract class. But since a static method can be called directly, making it abstract would make it possible to call an undefined method.

abstract class Foo {

abstract static void bar();

}

// Calling a method with no body!

Foo.bar();

In an interface, all methods are implicitly abstract. This is why an interface cannot declare a static method. (There's no architectural reason why an interface couldn't have a static method, but I suspect the writers of the JLS felt that that would encourage misuse of interfaces)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值