Sobes.tech
Junior — Middle

Can attributes be declared inside an interface?

sobes.tech AI

Answer from AI

In Java, interfaces cannot contain regular attributes (instance variables) because they do not have state. However, interfaces can declare:

  • Constants — these are public static final fields. By default, all fields in an interface are public static final.

For example:

public interface MyInterface {
    int CONSTANT_VALUE = 10; // public static final by default
}

Thus, declaring non-static fields (instance attributes) inside an interface is not allowed. An interface only describes behavior (methods) and constants.

Can attributes be declared inside an interface? — Java - sobes.tech