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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:51:11+00:00 2026-05-25T16:51:11+00:00

My BigBlock class needs a few overloaded constructors. All of them need to initialize

  • 0

My BigBlock class needs a few overloaded constructors. All of them need to initialize the same few fields in the same way.

What is the proper way to do this? Is it to make a function, e.g. Initialize in the example below, that does these things, and have all constructors call that function?

public class BigBlock {
    private Thing parentThing;
    Units lengthUnit;
    LabCoordinateSystem labCoordinateSystem;

    private void Initialize(){
        lengthUnit = parentThing.getPreferredUnits(0);
        labCoordinateSystem = parentThing.getCoordinateSystem();
    }

    BigBlock(Thing myThing){
        parentThing= myThing;
        Initialize();
    }

    BigBlock(Thing myThing, double x, double y, double z){
        parentThing= myThing;
        Initialize();
        // more code involving x, y, z
    }

    // a few more constructors

}
  • 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-25T16:51:11+00:00Added an answer on May 25, 2026 at 4:51 pm

    Typically it’s best to make all constructors chain to a single one which contains the most information, e.g.

    BigBlock(Thing myThing) {
        this(myThing, 0, 0, 0); // Assuming 0 is the default value for x, y and z
    }
    

    It becomes slightly weirder if there are different ways to call the constructor which don’t effectively represent subsets of the same information – but at that point I’d say there’s a design smell anyway.

    Note that by the time you’ve got all the real logic in a single constructor, you don’t need your Initialize method (which should be initialize to follow Java naming conventions, btw) at all – which may also you can make fields final which previously you couldn’t have done.

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

Sidebar

Related Questions

Hi LaTeX enthusiasts and TeX programmers! I'm currently developing a single-page document class for
I have a fictional class: template<typename T> class demonstration { public: demonstration(){} ... T
I need to send a big block of HTML in a Json object like
My application needs lots of memory and big data structure in order to perform
I need to maintain an xml column in a SQL database. a) in my
I'm in the process of creating a class that stores metadata about a particular
I need to merge two doubly-linked lists, but not by their values (the lists
exampleObject = new Panel{ if (this.exampleField ===1) { } } Cursor is on one
This is a very weird bug I'm having. I'm using Modernizer and after a
<?php /* /* this is a comment */ */ ?> PHP returns "syntax error"...

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.