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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:38:21+00:00 2026-05-13T18:38:21+00:00

Let’s suppose that I have the following class which tries to be immutable public

  • 0

Let’s suppose that I have the following class which tries to be immutable

    public class Computation {

    private final Operation operation;
    private final double epsilon;

    public Computation(Operation operation) {
        this.operation = operation;
        //Default value
        epsilon = 0.01;

    }


    public Computation(Operation operation double epsilon) {
        this(operation);
        //Won't compile as epsilon is final and is set by the other constructor
        this.epsilon = epsilon;
    }
}

And, for the sake of the question, let’s assume that I do not want to use a builder for this class (which would solve the issue).

So the question is:

Is there a way to achieve this behaviour without removing the final modifier to epsilon and keeping the two constructors?

That is, without doing something like

       public class Computation {

        private final Operation operation;
        private final double epsilon;

        public Computation(Operation operation Double epsilon) {
            this(operation);
            this.epsilon = (epsilon == null) ? 0.01 : epsilon;
        }
    }

and without using a builder

   public class Computation {

    private final Operation operation;
    private final double epsilon;

    private Computation(Builder builder) {
        this.operation = builder.operation;
        this.epsilon = builder.epsilon;

    }


    public static class Builder {

    private final Operation operation;
    //Default value
    private double epsilon = 0.01;

    public Builder(Operation operation) {
        this.operation = operation;
    }

    public Builder epsilon(double epsilon) {
         this.epsilon = epsilon;
         return this;
    }

    public Computation build() {
        return new Computation(this);
    }

}
  • 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-13T18:38:21+00:00Added an answer on May 13, 2026 at 6:38 pm

    Yes – reverse the logic so that your constructor with fewer parameters calls the one with more:

    public Computation(Operation operation) {
        this(operation, 0.01);
    }
    
    public Computation(Operation operation, double epsilon) {
        this.operation = operation;
        this.epsilon = epsilon;
    }
    

    Basically that way you can end up with quite a few constructors which all just delegate to one “real” constructor which does all the actual work.

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

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
Let's say we have the following: abstract class A; class B : public A;
Let's suppose I have a Discussion model, and that a discussion can be tagged.
Let's imagine I have a Java class of the type: public class MyClass {
Let's say for example, I have the following javascript function that returns a boolean:
Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):
let's say i have this public class MyClass { public string myMessage { get;
Let's say I have the following classes: class Votable(models.Model): name = ... class Vote(models.Model):
Let me try to explain what I need. I have a server that is
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a

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.