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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:41:37+00:00 2026-05-26T16:41:37+00:00

The code below shows a rectangle class using double points, which are also stored

  • 0

The code below shows a rectangle class using double points, which are also stored in an object. The rectangle and the points in the rectangle are immutable because they do not need to change. I would like to provide the ability to copy (make new point objects) or make a reference to the points provided in the constructor, but the only way I could think of doing this is to add a boolean parameter specifying if the caller wants to make a copy or reference.

This is for extensibility, although it may not be of prime importance, I would like this option. However, I don’t like the way it’s implemented with the boolean parameter. Is there a way I can make two constructors taking the same parameters, one to make a reference and one to make copies? or is there an equivalent to C++ parameter auto definition in the prototype so it doesn’t need to be specified by the caller? I have thought about using varargs, but then the caller could send unlimited parameters as garbage, possibly causing a stack overflow, I think…

/**
 * An immutable, double-precision floating-point (64-bit doubles x4) rectangle.
 * The rectangle can be made to reference existing points, or to create new points. Since the points
 * are also immutable, this is acceptable, as it is guaranteed they cannot change.
 * @author Bill
 */
public class DoubleRect {
    public final DoublePoint topLeft;
    public final DoublePoint bottomRight;

    public DoubleRect(DoublePoint setTopLeft, DoublePoint setBottomRight, boolean makeCopies) {
        if(makeCopies == true) {        
            topLeft = new DoublePoint(setTopLeft);
            bottomRight = new DoublePoint(setBottomRight);
        }
        else {
            topLeft = setTopLeft;
            bottomRight = setBottomRight;
        }
    }


}

UPDATE: Thanks to all that helped me figure out what to do instead. This is how I recoded it.

/**
 * An immutable, double-precision floating-point (64-bit) rectangle.
 * The rectangle can be made to reference existing points, or to create new points. Since the points
 * are also immutable, referencing the points is acceptable, as it is guaranteed they cannot change.
 * @author Bill
 */
public class DoubleRect {
    public final DoublePoint topLeft;
    public final DoublePoint bottomRight;

    /**
     * This constructor will reference the passed objects rather than duplicating them.
     * See the static factory method createWithClonedPoints() for making internal copies of the point objects
     * @param setTopLeft Double point designating the top left coordinate
     * @param setBottomRight Double point designating the bottom right coordinate
     */
    public DoubleRect(DoublePoint setTopLeft, DoublePoint setBottomRight) {
        topLeft = setTopLeft;
        bottomRight = setBottomRight;
    }

    /**
     * This constructor will create new immutable points within this object using the coordinates specified
     */
    public DoubleRect(double top, double left, double right, double bottom) {
        topLeft = new DoublePoint(left, top);
        bottomRight = new DoublePoint(right, bottom);
    }

    public static DoubleRect createWithClonedPoints(DoublePoint topLeft, DoublePoint bottomRight) {
        return new DoubleRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
    }


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

    Is there a way I can make two constructors taking the same parameters, one to make a reference and one to make copies?

    In such a case, it is advised to have multiple static-factory-methods instead of constructors.

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

Sidebar

Related Questions

The code below shows a small WinForms app which includes a simple Control that
The code below shows a sample that I've used recently to explain the different
The code below shows me (I think) that the for each loop is about
My PHP code below shows an image on browser as its original size 300x300.
The code below compiled in Debug configuration in VS2005 SP1 shows two messages with
I have the code below that hides and shows the navigational bar. It is
I have noticed that my geocoder is inconsistent in the code shown below because
Okay, so I've got the following code shown below to create a dialog using
The code below shows a line in different colors, but that's a lot of
The code below shows nested while loops, but it's not the very efficient. Suppose

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.