GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • ←
  • →

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — Static methods in interfaces | java.evolved

Home / Language / Static methods in interfaces
Language Beginner

Static methods in interfaces

  • 🐛 Report a code issue
  • 🌐 Report a translation issue
  • 💡 Suggest a new pattern

Add static utility methods directly to interfaces instead of separate utility classes.

Interfaces
Code Comparison
✕ Java 7
// Separate utility class needed
public class ValidatorUtils {
    public static boolean isBlank(
        String s) {
        return s == null ||
               s.trim().isEmpty();
    }
}

// Usage
if (ValidatorUtils.isBlank(input)) { ... }
✓ Java 8+
public interface Validator {
    boolean validate(String s);

    static boolean isBlank(String s) {
        return s == null ||
               s.trim().isEmpty();
    }
}

// Usage
if (Validator.isBlank(input)) { ... }
Share 𝕏
See a problem with this code? Let us know.
Why the modern way wins
📦

Better organization

Keep related utilities with the interface, not in a separate class.

🔍

Discoverability

Factory and helper methods are found where you'd expect them.

🧩

API cohesion

No need for separate *Utils or *Helper classes.

Old Approach
Utility classes
Modern Approach
Interface static methods
Since JDK
8
Difficulty
Beginner
JDK Support
Static methods in interfaces
Available

Available since JDK 8 (March 2014)

How it works

Before Java 8, utility methods related to an interface had to live in a separate class (e.g., Collections for Collection). Static methods in interfaces let you keep related utilities together. Common in modern APIs like Comparator.comparing(), Stream.of(), and List.of().

Related Documentation
Evolving Interfaces (dev.java) ↗
Proof
View proof source ↗
Related patterns
Language Intermediate

Default interface methods

Java 7
// Need abstract class to share behavior
public abstract class AbstractLogger {
    public void log(String msg) {
        System.out.println(
            timestamp() + ": " + msg);
    }
    abstract String timestamp();
}

// Single inheritance only
public class FileLogger
    extends AbstractLogger { ... }
Java 8+
public interface Logger {
    default void log(String msg) {
        IO.println(
            timestamp() + ": " + msg);
    }
    String timestamp();
}

// Multiple interfaces allowed
public class FileLogger
    implements Logger, Closeable { ... }
Hover to see modern ➜
JDK 8+ →
Language Intermediate

Private interface methods

Java 8
interface Logger {
    default void logInfo(String msg) {
        System.out.println(
            "[INFO] " + timestamp() + msg);
    }
    default void logWarn(String msg) {
        System.out.println(
            "[WARN] " + timestamp() + msg);
    }
}
Java 9+
interface Logger {
    private String format(String lvl, String msg) {
        return "[" + lvl + "] " + timestamp() + msg;
    }
    default void logInfo(String msg) {
        IO.println(format("INFO", msg));
    }
    default void logWarn(String msg) {
        IO.println(format("WARN", msg));
    }
}
Hover to see modern ➜
JDK 9+ →
Collections Beginner

Immutable list creation

Java 8
List<String> list =
    Collections.unmodifiableList(
        new ArrayList<>(
            Arrays.asList("a", "b", "c")
        )
    );
Java 9+
List<String> list =
    List.of("a", "b", "c");
Hover to see modern ➜
JDK 9+ →
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.