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 134051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:32:37+00:00 2026-05-11T06:32:37+00:00

I am reading Josh Bloch’s book Effective Java and he suggests using a builder

  • 0

I am reading Josh Bloch’s book Effective Java and he suggests using a builder design pattern when building objects that have large amounts of members. From what I can see it isn’t the vanilla design pattern but looks like his variation. I rather like the look of it and was trying to use it in a C# web application that I am writting. This is the code written in Java and works perfectly

public class Property {       private String title;     private String area;      private int sleeps = 0;      public static void main(String[] args) {          Property newProperty = new Property.Builder('Test Property').Area('Test Area').Sleeps(7).build();      }      private Property(Builder builder) {         this.title = builder.title;         this.area = builder.area;         this.sleeps =builder.sleeps;     }      public static class Builder{         private String title;         private String area;          private int sleeps = 0;          public Builder (String title){             this.title = title;         }          public Builder Area(String area){             this.area = area;             return this;         }          public Builder Sleeps(int sleeps){             this.sleeps = sleeps;             return this;         }          public Property build() {             return new Property(this);         }     }    } 

When I put this into what I think is the C# equivalent

 public class Property     {     private String title;     private String area;      private Property(Builder Builder)     {         title = Builder.title;         area = Builder.area;     }       public static class Builder     {         // Required parameters         private String title;         private String area;          // Optional parameters         private int sleeps = 0;          public Builder(String val)         {             this.title = val;         }          public Builder Area(String val)         {             this.area = val;             return this;         }          public Builder Sleeps(int val)         {             this.sleeps = val;             return this;         }          public Property build()         {             return new Property(this);         }     }     } 

Then I get compiler warnings. Most of them ‘cannot declare instance members in a static class’.

So my question is firstly what have I missed? If I have missed something, can I do it in the manner Josh Bloch recommends but in C#, and lastly, and this one important too, is this thread-safe?

  • 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. 2026-05-11T06:32:37+00:00Added an answer on May 11, 2026 at 6:32 am

    I think you can achieve pretty much the same effect if you create Builder as a top level class ( for that’s exactly what it is in Java ) and create a factory method to receive the builder in order to keep the constructor private ( which in turn would let you return subclasses instances if needed).

    The point is to let the builder perform the steps needed to create the object.

    So ( without knowing much about C# you could try something like this )

    // My naive C# attempt:P  public class Property  {      public static void main( String []args )      {          Property p = Property.buildFrom( new Builder('title').Area('area').Etc() )     }     public static Property buildFrom( Builder builder )      {         return new Propert( builder );     }      private Property ( Builder builder )      {         this.area = builder.area;         this.title = builder.title;         // etc.      } } public class Builder  {     public Builder ( String t )      {        this.title = t;     }      public Builder Area( String area )     {        this.area = area;        return this;     }     // etc.  } 

    The whole point of having Builder as an static inner class of property is to create a high coupling among the two ( as if they where one ). That’s why build method in Builder calls the private ‘Property’ constructor.

    Probably in C# you could use an alternate artifact to create the same coupling.

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Place a 16x16 favicon.ico file in your websites root directory.… May 11, 2026 at 11:43 pm
  • Editorial Team
    Editorial Team added an answer In the main thread, before spawning the background thread, save… May 11, 2026 at 11:43 pm
  • Editorial Team
    Editorial Team added an answer I think it's something like this: SELECT uid FROM table-b… May 11, 2026 at 11:43 pm

Related Questions

I am reading Josh Bloch's book Effective Java and he suggests using a builder
Important: This question is getting quite long, if this is the first time you're
I am reading a binary file into a parsing program. I will need to
I am reading in CSV file and translating it to an SQL Table. The
I am reading a .NET book, and in one of the code examples there

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.