title: How to initialize a variable in the constructor only once?
body: <p>I have a builder pattern in which I am taking few parameters from the customer and basis on that I am building my builder class and then that builder class is passed to our underlying library and then my library will use it.</p>
  
  <pre><code>public final class KeyHolder {
    private final String clientId;
    private final String deviceId;
    private final int processId;
    private final Cache&lt;String, List&lt;Response&gt;&gt; userCache;
    private static final long MAXIMUM_CACHE_SIZE = 5000000;
    private static final long EXPIRE_AFTER_WRITE = 120; // this is in seconds
  
    private KeyHolder(Builder builder) {
      this.clientId = builder.clientId;
      this.deviceId = builder.deviceId;
      this.processId = builder.processId;
      this.maximumCacheSize = builder.maximumCacheSize;
      this.expireAfterWrite = builder.expireAfterWrite;
  
      // how to execute this line only once
      this.userCache =
          CacheBuilder
              .newBuilder()
              .maximumSize(maximumCacheSize)
              .expireAfterWrite(expireAfterWrite, TimeUnit.SECONDS)
              .removalListener(
                  RemovalListeners.asynchronous(new CustomListener(),
                      Executors.newSingleThreadScheduledExecutor())).build();
  
    }
  
    public static class Builder {
      protected final int processId;
      protected String clientId = null;
      protected String deviceId = null;
      protected long maximumCacheSize = MAXIMUM_CACHE_SIZE;
      protected long expireAfterWrite = EXPIRE_AFTER_WRITE;
  
  
      public Builder(int processId) {
        this.processId = processId;
      }
  
      public Builder setClientId(String clientId) {
        this.clientId = clientId;
        return this;
      }
  
      public Builder setDeviceId(String deviceId) {
        this.deviceId = deviceId;
        return this;
      }
  
      public Builder setMaximumCacheSize(long size) {
        this.maximumCacheSize = size;
        return this;
      }
  
      public Builder setExpiryTimeAfterWrite(long duration) {
        this.expireAfterWrite = duration;
        return this;
      }
  
      public KeyHolder build() {
        return new KeyHolder(this);
      }
    }
  
   // getters here
  }
  </code></pre>
  
  <p>For each and every call to our library they create a new <code>KeyHolder</code> builder class everytime and pass it to our library. <code>processId</code>, <code>clientId</code>, <code>deviceId</code> will change with every call but <code>maximumCacheSize</code> and <code>expireAfterWrite</code> will stay same as it with every call. As you can see above, I am using guava cache here and since they are creating <code>KeyHolder</code> builder class everytime how can I make sure that the below line is executed only once in my constructor?</p>
  
  <pre><code>    this.userCache =
          CacheBuilder
              .newBuilder()
              .maximumSize(maximumCacheSize)
              .expireAfterWrite(expireAfterWrite, TimeUnit.SECONDS)
              .removalListener(
                  RemovalListeners.asynchronous(new CustomListener(),
                      Executors.newSingleThreadScheduledExecutor())).build();
  </code></pre>
  
  <p>Since with the current code right now, it will get executed with every call and I will get a new guava cache every time in my library so whatever entry was cached earlier within my library by using this guava cache will get lost.</p>
  
  <p>How to initialize a particular variable only once and after that it should ignore the value whatever is being passed to it?</p>
  
  <p><strong>Update:</strong></p>
  
  <pre><code>public class DataClient implements Client {
      private final ExecutorService executor = Executors.newFixedThreadPool(10);
  
      // for synchronous call
      @Override
      public List&lt;Response&gt; executeSync(KeyHolder key) {
          Cache&lt;String, List&lt;Response&gt;&gt; userCache = key.getUserCache();
          List&lt;Response&gt; response = userCache.getIfPresent(key.getUUID());
          if (CollectionUtils.isNotEmpty(response)) {
            return response;
          }
          // if not in cache, then normally call the flow and populate the cache
          List&lt;Response&gt; dataResponse = null;
          Future&lt;List&lt;Response&gt;&gt; future = null;
          try {
              future = executeAsync(key);
              dataResponse = future.get(key.getTimeout(), TimeUnit.MILLISECONDS);
              userCache.put(key.getUUID(), dataResponse);
          } catch (TimeoutException ex) {
              // log error and return DataResponse
          } catch (Exception ex) {
              // log error and return DataResponse
          }
  
          return dataResponse;
      }
  }
  </code></pre>