How to specify function types for void not Void methods in Java8?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Java 8 introduced a new paradigm to handle functions and methods, making it possible to manipulate them as first-class citizens. One of the powerful features introduced was the support for lambda expressions and functional interfaces, which allow methods to be referenced and passed around more easily. When dealing with methods in Java 8, it’s essential to understand how to handle methods that don't return a value, i.e., those with a void return type.
Understanding Function Types and Lambda Expressions in Java
Java 8's functional programming features are built around the concept of functional interfaces. A functional interface is an interface with a single abstract method (SAM). These are integral to lambda expressions and method references:
- Lambda Expressions: Allow you to implement the abstract method of a functional interface using a concise expression.
- Method References: Provide a way to refer to methods directly via the method's name.
Specifying Function Types for void Methods
In Java, function types that return void are represented by the Consumer functional interface. The Consumer<T> interface performs an operation on a given argument without returning any result. Here's how you can specify function types for methods that return void using Consumer<T>.
Basic Example
Consider a simple example where you have a method that prints a message:
To specify a function type for this void method, you can use a Consumer<String>:
How Method References Work for void Methods
Method references simplify the implementation by providing direct references to existing methods, improving code readability without losing functionality.
instance::method— Refers to an instance method of a particular instance.ClassName::method— Refers to a static method or any instance method of a particular class.
Key Functional Interfaces for void Methods
While the Consumer interface is most often used for void methods, there are other relevant functional interfaces to be aware of:
BiConsumer<T, U>: Accepts two input arguments and performs an operation without returning a result, ideal forvoidmethods that accept two parameters.
Runnable: Does not take any input and does not return an output, useful for defining a process or task that executes separately without any argument.
Key Points Summary
Below is a table summarizing the key function types and their use cases in Java 8 for void methods:
| Functional Interface | Parameters | Usage |
Consumer<T> | T - Single parameter | Executes operations on a single parameter. |
BiConsumer<T, U> | T, U - Two parameters | Executes operations on two parameters. |
Runnable | No parameters | Executes a task or action without arguments. |
Additional Details
Error Handling in Function Types
When dealing with lambda expressions and method references that provide a void return type, it is crucial to handle exceptions properly since lambdas may only throw exceptions declared in the throws clause of the target method. You may choose to wrap lambda expressions in a try-catch block or use custom functional interfaces to handle checked exceptions.
Testing Strategy
For testing the void method functionality within lambda expressions, utilize mocking frameworks like Mockito in Java to verify the behavior of consumers and actions, enabling developers to focus solely on what should happen rather than the specific output.
By leveraging functional interfaces and understanding method references, you can elegantly handle void methods in Java 8, making them efficient, readable, and flexible for various scenarios.
Related reading
- How to specify jackson to only use fields - preferably globally
- How to specify Java version in Spring/Spring Boot pom.xml?
- How to specify packagesToScan in HibernateJpaAutoConfiguration?
- How to specify prefix for all controllers in Spring Boot?
- How to specify the JDK version in Android Studio?
- How to split a delimited string to a ListString
- How to split a String by space
- How to start @KafkaListener based on start flag

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.