Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’ll delve into the topic of a Holder<T> class in Java. Although not a built-in Java class, the Holder<T> concept can significantly enhance our development efficiency. Let’s understand the powerful aspects of Holder<T> and how it can enhance our code.

2. The Limitations of Pass-by-Value Semantics

To understand why we might need a Holder<T> class, let’s first consider a common scenario: passing a simple Boolean to a method. We’ll create a mock service method getSupplierByZipCode(), expecting it to modify the Boolean value:

public class SupplierService {
    public void getSupplierByZipCode(String zip, Boolean result) {
        if (zip.startsWith("9")) {
            result = true;
        } else {
            result = false;
        }
    }
}

Now, let’s test this service with a zipCode that starts with “9“, expecting the result to change to true:

@Test
public void givenValidZipCode_whenGetSupplierByZipCode_thenTrue() {
    SupplierService service = new SupplierService();
    Boolean result = false;
    String zipCode = "98682";
    service.getSupplierByZipCode(zipCode, result);
    assertTrue(result);
}

This test fails! Because of Java’s pass-by-value semantics, the result Boolean we pass into getSupplierByZipCode() isn’t actually altered by the method. When the method attempts to modify result, it’s only modifying a copy, leaving the original result unaltered.

This limitation is precisely what Holder<T> can help us overcome.

3. Conceptualizing Holder<T>

We can regard Holder<T> as a generic container or wrapper class capable of storing and managing an object of any type T.

It primarily exists to overcome Java’s pass-by-value semantics, providing an indirect way to mimic pass-by-reference behavior.

T here signifies a type parameter, meaning any valid Java reference type can replace it. This allows us to have one Holder class that can accommodate any data type. However, it’s important to note that Holder<T> shouldn’t be used indiscriminately for every scenario. Especially when dealing with immutable objects, using Holder<T> might not be the most efficient or recommended approach.

4. The Holder<T> Class

Let’s imagine we have a simple Holder class that wraps a value of type T. Here’s how we might define it:

public class Holder<T> {
    public T value;

    public Holder(T value) {
        this.value = value;
    }
}

In this example, Holder<T> acts as a container to hold and manage the value of any type T.

5. Using the Holder<T> Class

Now, let’s adapt our SupplierService to overcome the limitation we observed earlier with Java’s pass-by-value semantics. Instead of directly passing a Boolean to the getSupplierByZipCode() method, we’ll use the Holder<T> class. This allows the method to modify the Holder‘s value, simulating a scenario where we need to return additional information from a method apart from its return value.

public class SupplierService {
    public void getSupplierByZipCode(String zip, Holder<Boolean> resultHolder) {
        if (zip.startsWith("9")) {
            resultHolder.value = true;
        } else {
            resultHolder.value = false;
        }
    }
}

Let’s now re-run our tests with the modified SupplierService using the Holder<T>.

@Test
public void givenValidZipCode_whenGetSupplierByZipCode_thenTrue() {
    SupplierService service = new SupplierService();
    Holder<Boolean> resultHolder = new Holder<>(false);
    String zipCode = "98682";
    service.getSupplierByZipCode(zipCode, resultHolder);
    assertTrue(resultHolder.value);
}

@Test
public void givenInvalidZipCode_whenGetSupplierByZipCode_thenFalse() {
    SupplierService service = new SupplierService();
    Holder<Boolean> resultHolder = new Holder<>(true);
    String zipCode = "12345";
    service.getSupplierByZipCode(zipCode, resultHolder);
    assertFalse(resultHolder.value);
}

This time, our tests pass. The Holder<T> class, by providing an extra layer of indirection, allows us to mimic the behavior of pass-by-reference semantics and modify our desired variable within the getSupplierByZipCode() method.

6. Conclusion

In this article, we saw that the Holder<T> class can serve as a flexible and powerful tool for various programming scenarios in Java. Even though it’s not a built-in class, the Holder<T> concept provides an elegant way to create flexible and reusable code that can handle different types. This can enable us to overcome the limitation of Java’s call-by-value semantics in certain situations.

As always, the code for these examples is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
3 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.