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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:42:45+00:00 2026-06-07T03:42:45+00:00

I have a class with lots of fields. They should basically be set at

  • 0

I have a class with lots of fields. They should basically be set at the constructor phase and never change. Semantically the class then is an immutable one.

public class A{
    final int a;
    final short b;
    final double e;
    final String f;
    final String g;
    //and more
}

The problem is that normally these fields have default values and therefore I do not want to always burden the user with a constructor with all of them. Most time, they just need to set a couple of them. There are a couple of ways to solve this:

  1. I would need lots of constructor with different signature.
  2. Create a bunch of set method of these field and only set those non-default value. But this somehow indicate a different semantics other than immutable nature.
  3. Create a new parameter class that is mutable and use that class as constructor.

None of that is totally satisfactory. Is there any other approach? Thanks.
One way

  • 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-07T03:42:46+00:00Added an answer on June 7, 2026 at 3:42 am

    I would use a combination of a parameter class and a fluent builder API for creating the parameter:

    public class A {
        private final int a;
        private final short b;
        private final double e;
        private final String g;
    
        public static class Aparam {
            private int a = 1;
            private short b = 2;
            private double e = 3.141593;
            private String g = "NONE";
    
            public Aparam a(int a) {
                this.a = a;
                return this;
            }
    
            public Aparam b(short b) {
                this.b = b;
                return this;
            }
    
            public Aparam e(double e) {
                this.e = e;
                return this;
            }
    
            public Aparam g(String g) {
                this.g = g;
                return this;
            }
    
            public A build() {
                return new A(this);
            }
        }
    
        public static Aparam a(int a) {
            return new Aparam().a(a);
        }
    
        public static Aparam b(short b) {
            return new Aparam().b(b);
        }
    
        public static Aparam e(double e) {
            return new Aparam().e(e);
        }
    
        public static Aparam g(String g) {
            return new Aparam().g(g);
        }
    
        public static A build() {
            return new Aparam().build();
        }
    
        private A(Aparam p) {
            this.a = p.a;
            this.b = p.b;
            this.e = p.e;
            this.g = p.g;
        }
    
        @Override public String toString() {
            return "{a=" + a + ",b=" + b + ",e=" + e + ",g=" + g + "}";
        }
    }
    

    Then create instances of A like this:

    A a1 = A.build();
    A a2 = A.a(7).e(17.5).build();
    A a3 = A.b((short)42).e(2.218282).g("fluent").build();
    

    Class A is immutable, the parameters are optional, and the interface is fluent.

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

Sidebar

Related Questions

I have some class with lots of fields; public class CrowdedHouse { public int
I have a Django my_forms.py like this: class CarSearchForm(forms.Form): # lots of fields like
I have lots(+2000) of GUIDs(in some network class) and my program must find one
I have a .py file with lots of classes: class First(Second): #code class Third(Fourth):
I have class LegacyClass which inherits OldBaseClass. I'm considering a change to introduce a
I have a class with lots of properties. A shallow copy is enough to
I have a large class with lots of methods (25, interrelated of course). I'm
We have seen lots of discussion in SO regarding the class vs struct in
I've got a large, abstract, class InfoBase with lots of properties. Then there are
I have the following code: # app/models/part.rb class Part < ActiveRecord::Base has_many :lots #

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.