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 finalfields. By default, all fields in an interface arepublic 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.