Sitelet https://github.com/javaevolved/javaevolved.github.io/pull/198/files
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions content/language/anonymous-classes-to-lambdas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
id: bbccea66-79bb-4bf9-80ea-e6bdd96a7bfc
slug: "anonymous-classes-to-lambdas"
title: "Anonymous classes to lambdas and method references"
category: "language"
difficulty: "beginner"
jdkVersion: "8"
oldLabel: "Anonymous class"
modernLabel: "Java 8+"
oldApproach: "Anonymous class"
modernApproach: "Method reference"
oldCode: |-
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
save(event);
}
});
modernCode: |-
button.addActionListener(this::save);
summary: "Replace single-method anonymous classes with concise lambdas and method references."
explanation: "Functional interfaces can be implemented with lambdas or method references. This removes anonymous-class ceremony while preserving static typing and the same callback contract."
whyModernWins:
- icon: "⚡"
title: "Less boilerplate"
desc: "Removes the class declaration and override ceremony."
- icon: "👁"
title: "Better readability"
desc: "Keeps attention on the operation being performed."
- icon: "🔒"
title: "Still type-safe"
desc: "The compiler checks the target functional-interface signature."
support:
state: "available"
description: "Available since JDK 8 (March 2014)."
prev: "tooling/junit6-with-jspecify"
next: "language/default-interface-methods"
related:
- "language/default-interface-methods"
- "streams/predicate-not"
- "collections/collection-bulk-operations"
tags:
- functional
- interfaces
docs:
- title: "Lambda Expressions"
href: "https://dev.java/learn/lambdas/"
- title: "Method References"
href: "https://dev.java/learn/lambdas/method-references/"
2 changes: 1 addition & 1 deletion content/language/default-interface-methods.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ whyModernWins:
support:
state: "available"
description: "Available since JDK 8 (March 2014)."
prev: "tooling/junit6-with-jspecify"
prev: "language/anonymous-classes-to-lambdas"
next: "language/markdown-javadoc-comments"
related:
- "language/private-interface-methods"
Expand Down
2 changes: 1 addition & 1 deletion content/tooling/junit6-with-jspecify.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ support:
state: "available"
description: "Available since JUnit 6.0 (October 2025, requires Java 17+)"
prev: "tooling/aot-class-preloading"
next: "language/default-interface-methods"
next: "language/anonymous-classes-to-lambdas"
related:
- "enterprise/spring-null-safety-jspecify"
- "errors/helpful-npe"
Expand Down
16 changes: 16 additions & 0 deletions proof/language/AnonymousClassesToLambdas.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 25+
import java.awt.event.*;

/// Proof: anonymous-classes-to-lambdas
/// Source: content/language/anonymous-classes-to-lambdas.yaml
void save(ActionEvent event) {}

void main() {
Button button = new Button();
button.addActionListener(this::save);
}

class Button {
void addActionListener(ActionListener listener) {}
}