Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1081051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:07:01+00:00 2026-05-16T22:07:01+00:00

I use Java builder pattern introduced by Joshua Bloch. Sometimes, I find certain fields

  • 0

I use Java builder pattern introduced by Joshua Bloch. Sometimes, I find certain fields which is more expensive to be initialized with a default value compared to primitive types.

Hence, what my strategy is that.

  1. I delay the default value initialization operation for those fields.
  2. During build, I will only initialize them to default value, if they are not set by caller before.

I am not sure whether it is good to do so? Is there any catch may happen? Like, thread safety issues? So far, I do not see any issue with this.

package sandbox;

import java.util.Calendar;

/**
 *
 * @author yccheok
 */
// Builder Pattern
public class NutritionFacts {
    private final int servingSize;
    private final int servings;
    private final int calories;
    private final int fat;
    private final int sodium;
    private final int carbohydrate;

    private final java.util.Calendar calendar;    // !!!

    public static class Builder {
        // Required parameters
        private final int servingSize;
        private final int servings;
        // Optional parameters - initialized to default values
        private int calories = 0;
        private int fat = 0;
        private int carbohydrate = 0;
        private int sodium = 0;

        // We suppose to provide a default value for calendar. However, it may
        // seem expensive. We will do it later during build.        
        private java.util.Calendar calendar = null;

        public Builder(int servingSize, int servings) {
            this.servingSize = servingSize;
            this.servings = servings;
        }
        public Builder calories(int val)
        { calories = val; return this; }
        public Builder fat(int val)
        { fat = val; return this; }
        public Builder carbohydrate(int val)
        { carbohydrate = val; return this; }
        public Builder sodium(int val)
        { sodium = val; return this; }

        public NutritionFacts build() {
            // !!!
            if (this.calendar == null) {
                this.calendar = Calendar.getInstance();
            }            
            return new NutritionFacts(this);
        }
    }

    private NutritionFacts(Builder builder) {
        servingSize = builder.servingSize;
        servings = builder.servings;
        calories = builder.calories;
        fat = builder.fat;
        sodium = builder.sodium;
        carbohydrate = builder.carbohydrate;
        calendar = builder.calendar;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:07:02+00:00Added an answer on May 16, 2026 at 10:07 pm

    what if I really want the calendar to be null? Your pattern won’t work then. Otherwise, it seems OK for me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Joshua Bloch's Effective Java describes a Builder Pattern that can be used to build
In Java, you can use the builder pattern to provide a more readable means
I develop Flex-Java applications which is running under Apache Tomcat. I use Flex Builder
How can I use java reflection or similar to find project name by just
Can you please recommend a fast XML Builder in Java that doesn't use annotations?
I use builder for my buildprocess. I have some java classes in my src
According to Effective Java 2ed Item 2 telescoping constructor pattern , in which you
I am reading Josh Bloch's book Effective Java and he suggests using a builder
I am currently using the Builder pattern, following closely the Java implementation suggested in
I use java and a regexp. I've made a regexp for password validation :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.