HRS - Ask. Learn. Share Knowledge. Logo

In Computers and Technology / High School | 2025-07-08

Refer to the code given below:

```java
public class Student {
public static void borrowBook(Integer isbn) {
System.out.println("Student borrowed the book with "+isbn);
}
}
```

Identify the correct option to create a method reference to static method inside the Student class. [Assume Java 8 is used]

A. BiFunction transaction = Student::borrowBook;

B. BiConsumer transaction = Student::borrowBook;

C. Consumer transaction = Student::borrowBook;

D. Predicate transaction = Student::borrowBook;

Asked by scully4279

Answer (2)

The correct option to create a method reference to the static method 'borrowBook' in the 'Student' class is B. BiConsumer transaction = Student::borrowBook, since it can take one parameter and return void. Other options do not match the method's signature or return type.
;

Answered by Anonymous | 2025-07-21

In this question, we're discussing method references in Java, a feature introduced in Java 8. The concept revolves around using a shorthand syntax to express a lambda expression that delegates an action to a static method.
Possible Function Interfaces:

BiFunction: This interface represents a function that accepts two arguments and produces a result.

BiConsumer: This interface represents an operation that accepts two arguments and returns no result.

Consumer: This interface represents an operation that accepts a single argument and returns no result.

Predicate: This interface represents a boolean-valued function that takes one argument.


Analysis:
The method borrowBook in class Student is a static method that accepts a single argument of type Integer and returns void (it prints a message). The best functional interface matching this signature is Consumer , which takes one argument and does not return any value.
Conclusion:
Based on the method's signature and the characteristics of the provided functional interfaces, the correct option to create a method reference to the static method borrowBook is:
Consumer transaction = Student::borrowBook;

Answered by ElijahBenjaminCarter | 2025-07-21