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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:56:14+00:00 2026-06-14T05:56:14+00:00

So imagine we have a situation like this: public class Bar1 { public final

  • 0

So imagine we have a situation like this:

public class Bar1 {
    public final int VALUE1; 
    public final double VALUE2;
    public final String NAME; 
    /*DOZENS OF FINALS!*/

    public Bar1(int v1, double v2, String name)
    {
        this.VALUE1 = v1; 
        this.VALUE2 = v2;
        this.NAME = name; 
    }

}

and

public class Foo1
{
    private Bar1 bar1;
    private Bar1 bar2;
    private Bar1 bar3;
    /*Dozens of bars!*/

    public Foo1( //We need to pass the values in here)
                 {
                     bar1 = new Bar1( //we need to initialise each of the bars. )
                     bar2 =...
                     bar3 = ...
                 }


}

What’s a good way of doing this? I think passing each one in as it’s own argument,

eg:

public class Foo1
{
    private Bar1 bar1;
    private Bar1 bar2;
    private Bar1 bar3;
    /*Dozens of bars!*/

    public Foo1(int bar1Value1, double bar1Value2, String bar1String, 
                        int bar2Value1, double bar2Value2, String bar2String,
                        int bar3Value1, double bar3Value2, String bar3String)
                 {
                     bar1 = new Bar1(bar1Value1, bar1Value2, bar1String);
                     bar2 = new Bar1(bar2Value1, bar2Value2, bar2String);
                     bar3 = new Bar1(bar3Value1, bar3Value2, bar3String);
                 }


}

is a huge pain in the ass.

I’ll post the solution I’ve come up with, but I’m wondering what other solutions there are.

  • 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-14T05:56:15+00:00Added an answer on June 14, 2026 at 5:56 am

    Separate out your types. If you have dozens of fields, it’s almost certain that either:

    • Some of those fields are related to each other, so should be captured in their own type, e.g. if you have fields address1, address2, address3, city, state then that should be an Address type

    • Some of the fields aren’t related at all, and shouldn’t be in the same type to start with

    I have never seen a well-designed class with dozens of fields.

    Once you’ve cut down the number of fields, you won’t have a problem with an unwieldy constructor.

    If you do still have a lot of fields, the builder pattern (as mentioned in comments) is indeed good, if a little tedious: have a mutable builder type (often a nested type), and then a build() method which passes the builder itself to the constructor of the immutable type, which copies the values from the builder into the final fields.

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

Sidebar

Related Questions

In my code I have often situations like this: public void MyMethod(string data) {
Imagine situation like this: You have a working repo in bitbucket - you were
Let's imagine that I have something like this: <html> ... <div> <iframe src=test.html hash=r4d5f7></iframe>
let's imagine we have this situation: I have a lot of .RData files, which
Best way to describe this is explain the situation. Imagine I have a factory
I have problems with faceting. Imagine this situation. Product can be in more than
Imagine the following situation: I have two branches: DEV and MAIN. I'm working on
Probably a very basic beginner question. Imagine the following situation: I have an ASP.NET
imagine a situation where you have an application that needs to import data from
I have a question about jQuery load best practice. Imagine the following situation: $(#div).on('click',

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.