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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:22:58+00:00 2026-05-13T08:22:58+00:00

When designing classes you usually have to decide between: providing a full constructor that

  • 0

When designing classes you usually have to decide between:

  • providing a “full” constructor that takes the initial values for all required fields as arguments: clumsy to use but guarantees fully initialized and valid objects
  • providing just a “default” constructor and accessors for all necessary fields: might be convenient sometimes but does not guarantee that all members are properly initialized before some critical methods are called.
  • a mixed approach (more code, more work, can’ eliminate the “not fully initialized” problem)

I have seen several APIs and frameworks that use one of the above or even an inconsistent approach that differs from class to class. What are your thoughts and best practices on that subject?

  • 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-13T08:22:58+00:00Added an answer on May 13, 2026 at 8:22 am

    The short answer is that an object should be fully initialized after its constructor was called.

    This should be the default approach with the least surprises for users. There are cases when your run-time, framework or other technical constraints prevent the default approach.

    In some circumstances a the Builder pattern helps to support cases when it is not possible to use a simple constructor. This approach is in the middle ground, letting users call setters to initialize and still be able to work only with fully initialized objects.

    A static factory method is appropriate in cases when the object constructor needs to be more flexible than a constructor but a builder is too complex to implement.

    Constructor:

    x = new X(a, b);
    

    Setter:

    x = new X();
    x.setA(a);
    x.setB(b);
    

    Builder:

    builder = new Builder();
    builder.setA(a);
    builder.setB(b);
    x = builder.build();
    

    Static factory method:

    x = X.newX(a, b);
    

    All four approaches will produce an instance, x, of class X.

    Constructor

    Pros:

    • Simple

    Cons:

    • May be not possible due to constraints
    • May need too many constructor arguments (named arguments and default values can help here if the languages supports them: new X(a = "a", b = "c"))

    Setter

    Pros:

    • Moderate complexity
    • May be mandated by framework

    Cons:

    • Instances may not be fully initialized

    Builder

    Pros:

    • Most flexible approach
    • May reuse instances (using singletons, flyweights and caches internally)

    Cons:

    • Most complex to implement
    • May produce run-time exceptions for invalid initialized objects
    • Overhead of the builder object instance

    Static factory method

    Pros:

    • Moderate complexity
    • May reuse instances (using singletons, flyweights and caches internally)

    Cons:

    • More complex to implement and use than constructor
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 269k
  • Answers 269k
  • 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 WITH customers (id, CustomerStreet1, CustomerStreet2) AS ( SELECT 1, 'Some… May 13, 2026 at 1:21 pm
  • Editorial Team
    Editorial Team added an answer It's for databinding. When you bind the command to every… May 13, 2026 at 1:21 pm
  • Editorial Team
    Editorial Team added an answer It's not always 4, often it's 2. It is configurable… May 13, 2026 at 1:21 pm

Related Questions

I enjoy and highly recommend Juval Lowy's - C# Coding Standard . Juval explicitly
I'm learning about Memory management in C# from the book Professional C# The presence
When designing LINQ classes using the LINQ to SQL designer I've sometimes needed to
I'd like to know some best practice when designing c++ classes. To put it
How can you avoid circular dependencies when you're designing two classes with a producer/consumer

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.