Junior — Middle
Is it permissible to place constants in the interface definition?
sobes.tech AI
Answer from AI
In Java, it is permissible to place constants in an interface, since all fields in an interface are by default public static final. However, this approach is considered outdated and not recommended, as interfaces are intended to describe behavior, not to store data.
It is better to use a separate class with constants, for example:
public final class Constants {
public static final String ERROR_MESSAGE = "Error";
private Constants() {} // prevent instantiation
}
This improves code readability and architecture.