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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:22:49+00:00 2026-06-15T12:22:49+00:00

Is it better to use constructors with parameters to initialize the members of a

  • 0

Is it better to use constructors with parameters to initialize the members of a class or do that indirectly using setter functions? I did not get a clear answer on this question so far. Any insights would be appreciated.

  • 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-06-15T12:22:50+00:00Added an answer on June 15, 2026 at 12:22 pm

    It is almost certainly a matter of design choice or sometimes style. I would consider a number of things:

    1. Would constructing an object without setting the member variables leave the class in an invalid state? If so, you’ll need your constructor to take valid values for those members. Otherwise, you should be able to provide useful defaults.

    2. Would setting individual members of variables violate a class invariant? If so, they should not have their own setters.

    3. Is the user of this class likely to want to modify individual members? If so, then setters may be appropriate.

    4. Does it make conceptual sense to be able to change individual members of an object? I would argue that it makes no sense to be able to change a Person‘s date of birth. However, you could argue that changing a Person‘s name does make sense. It depends. Do you consider a Person whose name has changed to be a different Person in your system?

    5. Can you group setters together to be more useful? Instead of Rectangle::setX(int) and Rectangle::setY(int), does Rectangle::setPosition(int,int) make more sense? Perhaps even Rectangle::setPosition(Point) is better.

    In any case, a class whose full set of members are exposed through individual setters and getters is usually a code smell.

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

Sidebar

Related Questions

I have a class where I use this to initialize a void* pointer. But
I have a public class with 2 constructors: The default (without parameters), which is
I try to use a simple example for better undersanding: I've got a class
I have a class which I intend to use for performing calculations. I declare
When using IoC for dependency injection the most recommended use is constructor injection (as
Often a class has to be configured using a number of boolean options. What
Anyone know of a better way to create and initialize SqlParameter s in VB.NET?
i do not have any experience with virtual constructors which are available in Delphi.
The constructor of class StructComponent takes a different logic to initialize its member variables
Possible Duplicate: In STL maps, is it better to use map::insert than []? I

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.