All 15 Interfaces & Abstract Classes questions
More in Interfaces & Abstract Classes
Advertisement
Back to results for “Classes & Class Syntax”
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?

Beginner

Answer

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 is
Real-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.

Common follow-ups: Why did the language designers choose to make interface fields implicitly constants rather than allowing genuine mutable instance state?;Is declaring constants in an interface (rather than a dedicated final utility class) still considered good practice today?