Java › Interfaces & Abstract Classes
Can an interface declare instance fields, and if so, what implicit modifiers are automatically applied to any field declared in an interface?
BeginnerAnswer
Any field declared in an interface is implicitly public, static, and final, regardless of whether these modifiers are explicitly written -- meaning an interface cannot have genuine per-instance mutable state (every field is effectively a shared, immutable constant), which is a key structural distinction from an abstract class, whose fields are ordinary instance fields with whatever access modifier and mutability you choose to declare.
public interface Constants {
int MAX_RETRIES = 3; // implicitly public static final, even without writing those modifiers
String DEFAULT_NAME = "Guest";
}
// Equivalent to explicitly writing:
// public static final int MAX_RETRIES = 3;
System.out.println(Constants.MAX_RETRIES); // accessed like a static field, since that's exactly what it isReal-world example A library defining a set of shared configuration constants in an interface relies on the automatic public-static-final treatment, meaning every implementing class (and any other code referencing the interface directly) sees the exact same single, immutable, shared constant values rather than each implementation getting its own independently mutable copy.