Java Docs says interfaces cannot have fields. Why?

I read this one and it clearly states that One significant difference between classes and interfaces is that classes can have fields whereas interfaces cannot.How is this possible because the Java docs also say all fields in interface are public,static and final.

+4
source share
3 answers

How is this possible, because Java Docs also says that all the fields in the interface: public, staticand final.

From the Java Language Specification. Chapter 9. Interfaces. 9.3. Field (permanent) declarations (emphasis added):

Each field declaration in the body of the interface is implicit public , staticand final. It is allowed to over-specify any or all of these modifiers for such fields.

() , , , , ConstantModifier. >

, public. public, static final , . .

, :

interface Foo {
    String bar = "Hello World";
}

interface Foo {
    public String bar = "Hello World";
}

interface Foo {
    static bar = "Hello World";
}

-.

, , . , . :

interface Foo {
    //compiler error!
    private String bar = "Hello World";
}

bar public, .

+5

A static final , . , .

+3

object new, fields , . interface, fields. constants interface, fields : public static final

. jls

+2

Source: https://habr.com/ru/post/1541270/


All Articles