Java Language Interfaces Modifiers in Interfaces

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

The Oracle Java Style Guide states:

Modifiers should not be written out when they are implicit.

(See Modifiers in Oracle Official Code Standard for the context and a link to the actual Oracle document.)

This style guidance applies particularly to interfaces. Let's consider the following code snippet:

interface I {
    public static final int VARIABLE = 0;

    public abstract void method();

    public static void staticMethod() { ... }
    public default void defaultMethod() { ... }
}

Variables

All interface variables are implicitly constants with implicit public (accessible for all), static (are accessible by interface name) and final (must be initialized during declaration) modifiers:

public static final int VARIABLE = 0;

Methods

  1. All methods which don't provide implementation are implicitly public and abstract.
public abstract void method();
Java SE 8
  1. All methods with static or default modifier must provide implementation and are implicitly public.
public static void staticMethod() { ... }

After all of the above changes have been applied, we will get the following:

interface I {
    int VARIABLE = 0;
    
    void method();

    static void staticMethod() { ... }
    default void defaultMethod() { ... }
}


Got any Java Language Question?