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

  • Home
  • SEARCH
  • 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 776083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:23:40+00:00 2026-05-14T19:23:40+00:00

Possible Duplicate: Initialize class fields in constructor or at declaration? We are arguing about

  • 0

Possible Duplicate:
Initialize class fields in constructor or at declaration?

We are arguing about coding practices. The examples here are a little too simple, but the real deal has several constructors. In order to initialise the simple values (eg dates to their min value) I have moved the code out of the constructors and into the field definitions.

public class ConstructorExample
{
    string _string = "John";
}

public class ConstructorExample2
{
    string _string;

    public ConstructorExample2()
    {
        _string = "John";
    }
}

How should it be done by the book? I tend to be very case by case and so am maybe a little lax about this kind of thing. However i feel that occams razor tells me to move the initialisation out of multiple constructors. Of course, I could always move this shared initialisation into a private method.

The question is essentially … is initialising fields where they are defined as opposed to the constructor bad in any way?

The argument I am facing is one of error handling, but i do not feel it is relevant as there are no possible exceptions that won’t be picked up at compile time.

  • 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-14T19:23:41+00:00Added an answer on May 14, 2026 at 7:23 pm

    It’s not necessarily bad to initialize values outside of the constructor, and the problem you have here:

     string _string;
    
        public ConstructorExample2()
        {
            _string = "John";
        }
    

    Is that if you have multiple constructors you have to remember to either
    1. Reinitialize _string in every constructor
    2. Separate the logic out into a common method and call that method in every constructor
    3. Call the constructor with the logic in it, from the other constructors. (Chain the constructors)
    Now this isn’t necessarily a problem, but you have to remember to do it. By initializing it outside of the constructor, it’s done for you. It’s one less thing you need to remember to do.

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

Sidebar

Related Questions

Possible Duplicate: Best Practice: Initialize class fields in constructor or at declaration? I am
Possible Duplicate: Should I initialize variable within constructor or outside constructor I have here
Possible Duplicate: What does a colon following a C++ constructor name do? Class construction
Possible Duplicate: initialize a const array in a class initializer in C++ If I
Possible Duplicate: Where is the “proper” place to initialize class variables in AS3 I
Possible Duplicate: Initializing Class Fields at the Field Definition or in Class Contructor Setting
Possible Duplicate: Constructor initialization-list evaluation order While writing a C++ constructor for a class,
Possible Duplicate: Learning C++: polymorphism and slicing Array of polymorphic base class objects initialized
Possible Duplicate: Initializing private static members Why I can't initialize non-const static member or
Possible Duplicate: c++ multithread I use c++ to implement a thread class. The code

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.