Java 8 : Accessibility of Interface members

标签 java java-8 jls

辅助功能

Java 8 Says:

All members of interfaces lacking access modifiers are implicitly public

Java 7 says:

All members of interfaces are implicitly public.

当我尝试以下代码时:

public interface Test {
    protected int i = 10;
}

并编译,我得到了

Test.java:3: error: modifier protected not allowed here
        protected int i = 10;
                      ^

Java 版本: Java(TM) SE Runtime Environment (build 1.8.0-b129)

但是在上面的 Interface 成员声明中我不缺少访问修饰符 那么为什么我会收到这个错误。

最佳答案

您引用的是 Determining Accessibility with Names 上的 JLS 部分。

All members of interfaces lacking access modifiers are implicitly public

您真正需要查看的是关于 interface field declarations 的部分哪个州

ConstantDeclaration:
    {ConstantModifier} UnannType VariableDeclaratorList ;
ConstantModifier:
    Annotation public 
    static final

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.

所以这些是您可以使用的修饰符。您在上面引用的部分指的是当您不使用任何访问修饰符时,在这种情况下它将隐式为 public

关于Java 8 : Accessibility of Interface members,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22870346/

相关文章:

java - 为什么对父构造函数的调用不是编译器为内部类生成的构造函数中的第一个调用?

java - 同步块(synchronized block)中的意外代码

java - JLS:包关键字之前的修饰符

java - 如何在 Java 中使用两个参数创建 ObservableBooleanValue?

Java 8 - 永久代支持 - 替代方案

java - 有什么好的方法可以将多维数组转换为 List<List<T>>?

java - Java 编程语言中的编码

java - 为什么Java有replaceAll(regex, replacement)而不是replaceAll(regex, regex)?

java - 如何更改 HashMap 内的链接列表?

java - 如何创建作用域为全局但不应该是静态的变量