Course contentsShow
← Java
Lesson 491 of 2,87012. Abstract Classes and InterfacesFree lesson

Interface Constants: public static final by Default

Declaring constants in interfaces where all fields are implicitly public, static, and final.

Interface Constants: public static final by Default

What You'll Learn

You'll learn how Java interfaces automatically make all field declarations public, static, and final constants, saving you from typing those modifiers explicitly.


The Automatic Modifiers

When you declare a field in an interface, Java automatically treats it as:

  • public – accessible from anywhere
  • static – belongs to the interface itself, not to instances
  • final – cannot be reassigned after initialization

You don't need to write these modifiers yourself—Java adds them behind the scenes. This means interfaces are perfect places to define shared constants that classes can use.


Why Interfaces Work This Way

Interfaces define contracts. Since they can't have instance state (no objects are created from interfaces directly), any fields must be constants shared across all implementations. The public static final combination enforces this: the values are compile-time constants available to everyone.


Syntax Comparison

interface MathConstants {
    // These three declarations are IDENTICAL:
    double PI = 3.14159;
    public static final double E = 2.71828;
    static final double GOLDEN_RATIO = 1.618;
}

All three fields are implicitly public static final, whether you write those keywords or not. Most developers omit them for cleaner code.


Using Interface Constants

interface Colors {
    int RED = 0xFF0000;
    int GREEN = 0x00FF00;
    int BLUE = 0x0000FF;
}

class Display implements Colors {
    void showColor() {
        System.out.println(RED);        // Inherited access
        System.out.println(Colors.RED); // Explicit access
    }
}

Classes implementing the interface can reference the constants directly or through the interface name.


Key Takeaway:
All fields in Java interfaces are automatically public static final constants—you can omit these modifiers, and the values must be initialized at declaration time.