Skip to content

Commit 54ba552

Browse files
committed
Add another method reference case
1 parent a7fbe14 commit 54ba552

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/main/java/io/javaalmanac/snippets/language/MethodReferences.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ static void classMethod() {
3333
System.out.println("classMethod");
3434
}
3535

36+
static int classMethodWithReturn(String parameter) {
37+
System.out.println("classMethodWithReturn " + parameter);
38+
return 42;
39+
}
40+
3641
}
3742

3843
public static void main(String[] args) {
@@ -57,6 +62,11 @@ public static void main(String[] args) {
5762
Runnable runnable2 = Foo::classMethod;
5863
runnable2.run();
5964

65+
// Method return values are ignored if the functional interface method
66+
// has return type void
67+
Consumer<String> consumer2 = Foo::classMethodWithReturn;
68+
consumer2.accept("hello");
69+
6070
// Array constructor as reference
6171
IntFunction<Foo[]> function = Foo[]::new;
6272
Foo[] array = function.apply(42);

0 commit comments

Comments
 (0)