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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:57:20+00:00 2026-06-16T18:57:20+00:00

I have an object that contains several fields (ints, Strings, etc) but also a

  • 0

I have an object that contains several fields (ints, Strings, etc) but also a HashMap and an ArrayList. This objects holds parameters used to later build a database query but there are times when I need to re-use all of the exact same parameters for an additional query, except for different items in the ArrayList.

I’ve noticed that when I change what’s in the array list, it always changes the original object. I’ve figured out how to make a shallow copy by overriding the clone() method but the array list always remains shared by any copy of the object. Before diving into deep copies and the like I think I need advice on whether that’s the best route.

Here’s an example of the object I need to find a way to duplicate.

public class QueryParameters implements Cloneable {

    protected HashMap<String,String> foundArgs = new HashMap<String,String>();
    protected ArrayList<ActionType> action_types = new ArrayList<ActionType>();

    protected String lookup_type = "lookup";
    protected Location loc;
    protected Vector player_location;
    protected int id = 0;
    protected int radius;
    protected boolean allow_no_radius = false;
    protected String player;
    protected String world;
    protected String time;
    protected String entity;
    protected String block;
    // ... lots of getters/setters
}

I could always make a new instance and use getters/setters to transfer the data but that feels too verbose to me – there are about 15 fields I’d have to copy over and if I ever add new ones I’d need to remember to add them here.

What’s the best way for me to obtain a new instance/clone so that changes to action_types doesn’t affect the original object

  • 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-16T18:57:22+00:00Added an answer on June 16, 2026 at 6:57 pm

    I suggest writing a clone method that uses super.clone() to copy most of the fields, and then replaces the ArrayList in the clone. Something like this:

    import java.util.ArrayList;
    
    public class QueryParameters implements Cloneable {
      @Override
      public String toString() {
        return "QueryParameters [action_types=" + action_types + ", otherData="
            + otherData + "]";
      }
    
      protected ArrayList<String> action_types = new ArrayList<String>();
      protected String otherData;
    
      @Override
      public QueryParameters clone() throws CloneNotSupportedException {
        QueryParameters cloned = (QueryParameters) super.clone();
        cloned.action_types = new ArrayList<String>(action_types);
        return cloned;
      }
    
      public static void main(String[] args) throws CloneNotSupportedException {
        QueryParameters test = new QueryParameters();
        test.otherData = "a field";
        test.action_types.add("xyzzy");
        test.action_types.add("abcde");
        System.out.println("Original: " + test);
        QueryParameters clone = test.clone();
        clone.action_types.add("A new action type");
        System.out.println("Modified clone: " + clone);
        System.out.println("Original after clone modification: " + test);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scroll view which contains several objects like text fields,labels,switch etc..Now as
I have an object that contains a array. On initialization of this object, the
I have an object model that contains a class with several subclasses. The subclasses
I have an assembly which contains several UserControl objects that I want to be
I have created an object in R that contains several attributes. How can I
I have an NSMutableArray that contains objects of type Person. The Person object contains
I have a javascript object (that is instantiated several times) that contains setTimeout functions.
I have a numeric javascript array, that contains several objects with geodata in it.
I have a CSV that contains fields that need to go to several Salesforce
I have a model with an object that contains a collection like this: namespace

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.