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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:55:54+00:00 2026-06-11T13:55:54+00:00

I’m a bit of a Java N00B, but I am majorly confused about clone.

  • 0

I’m a bit of a Java N00B, but I am majorly confused about clone.

Consider the following incorrect class

class blah {
   public blah(Collection<Integer> c){
       member_collection = c.clone(); //the clone here is defensive
   }
   private final Collection<Integer> member_collection;
}

I think what I want to do is clear.

I understand why the clone() symbol is not found: clone is not a public method in any possibly Collection. However, in any concrete class I would want to use it is, and I want to just tell the compiler that.

I’m sure I’m not the first person to want to clone a generic collection, but the tutorials I have looked at only explain why you cannot clone a Collection, not how to get around the problem. I have tried hacky stuff with getClass() and casting, but those usually require me to make ugly try-catch blocks and it’s difficult to ensure exactly one initialization (the pointer member_collection) is final!

What I’d really like to do is somehow put that information into the type somehow. Is there a nice way to do this?

Thanks!

  • 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-11T13:55:55+00:00Added an answer on June 11, 2026 at 1:55 pm

    You’re better off copying like so:

    public class Blah {
        private final Collection<Integer> collection;
    
        public Blah(Collection<Integer> collection) {
            this.collection = new ArrayList<Integer>(collection);
        }
    }
    

    Or like so:

    public class Blah {
        private final Collection<Integer> collection = new ArrayList<Integer>();
    
        public Blah(Collection<Integer> collection) {
            this.collection.addAll(collection);
        }
    }
    

    This is the same as what you’re trying and guarantees that any order of the given Collection is preserved.

    Edit: If you absolutely need it to be the same implementation as the one given to you and you don’t want to rethink your design, then you can do some of the hacky reflection stuff and put it in a static method.

    public class Blah {
        private final Collection<Integer> collection;
    
        public Blah(Collection<Integer> collection) {
            this.collection = clone(collection);
        }
    
        @SuppressWarnings("unchecked")
        private static <T> T clone(T t) {
            try {
                Method cloneMethod = t.getClass().getDeclaredMethod("clone");
                return (T) cloneMethod.invoke(t);
            } catch (Exception ex) {
                // TODO Log ex
                return null;
            }
        }
    }
    

    Must of the concrete collections implement clone as a public method, so this should work for most of them. It will be null for the ones that don’t. You may want to just rethrow whatever exception you get instead.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question

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.