Java 中 modifer 'public' is reduntant for interface methods

               

http://androidren.com/index.php?qa=322&qa_1=java-%E4%B8%AD-modifer-public-is-reduntant-for-interface-methods

经常会看到接口上写了public修饰方法,然后IDE,比如:Eclipse或者IDEA经常会提示public是多余的。后来就查了一下。java默认接口的方法是public和abstract的,所以真没必要。同时,如果你使用private或者protected都会报错。

所以,安心删除public吧。

http://stackoverflow.com/questions/17011374/are-public-and-public-final-redundant-for-interface-methods

You cannot have a final method declared in an interface. Fields are always final but methods are always abstract (and never final). You cannot define an interface method that is to be implemented only by classes in the same package.* From section 9.3 of the Java Language Specification:

Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.

and from section 9.4:

Every method declaration in the body of an interface is implicitly public (§6.6).

Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.

It is permitted, but discouraged as a matter of style, to redundantly specify the public and/or abstract modifier for a method declared in an interface.

* As Paul Bellora points out in a comment, you can make the interface itself package-private (or protected, or even private) if you want to restrict its visibility.


In addition, an interface can contain constant declarations. All constant values defined in an interface are implicitly publicstatic, and final. Once again, you can omit these modifiers.

另外,所有常量值默认都是public static final,不用添加。

           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值