GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing javaevolved.dev — Static methods in interfaces | java.evolved
Language
Beginner
Static methods in interfacesAdd static utility methods directly to interfaces instead of separate utility classes. 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)) { ... }
See a problem with this code? Let us know.
Why the modern way wins
📦
Better organizationKeep related utilities with the interface, not in a separate class. 🔍
DiscoverabilityFactory and helper methods are found where you'd expect them. 🧩
API cohesionNo 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
Proof
Related patterns
Language
Intermediate
Default interface methodsJava 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 { ... }
JDK 8+
→
Language
Intermediate
Private interface methodsJava 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));
}
}
JDK 9+
→
Collections
Beginner
Immutable list creationJava 8
List<String> list =
Collections.unmodifiableList(
new ArrayList<>(
Arrays.asList("a", "b", "c")
)
);
Java 9+
List<String> list =
List.of("a", "b", "c");
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. |