Im playing around with the clone method of arraylist and have a few questions. This is what i did with it:

ObjectMaker item1 = new ObjectMaker();
        ObjectMaker item2 = new ObjectMaker();
        ObjectMaker item3 = new ObjectMaker();

        ArrayList<ObjectMaker> arrayList = new ArrayList<ObjectMaker>();
        arrayList.add(item1);
        arrayList.add(item2);
        arrayList.add(item3);

        Object theList = arrayList.clone();

I was suprised that clone return an object. I dont see the point since you cant use your methods on it since its of the class Object.
Also I cant seem to do anything with theList object. I am trying to get/use arraylist but its not working:

ArrayList<ObjectMaker> arrayList2 = theList.arrayList;

Dani AI

Generated

noticed that calling clone() on an ArrayList left him with an Object and no list methods — that’s expected. ArrayList.clone() is declared to return Object and it returns a shallow copy of the list (a new backing array that holds the same element references). That is why a cast is required to treat the result as a List/ArrayList. (docs.oracle.com)

is correct that casting the returned Object back to a list will let you use list methods, but that cast is unchecked (generics are erased at runtime) and can generate a compiler warning. A clearer, type-safe alternative is to use the collection-based constructor, which produces an equivalent shallow copy without any cast:

List<MyType> copy = new ArrayList<>(originalList);

This is simple and preserves compile-time generics. (docs.oracle.com)

Important distinction: both clone() and the copy-constructor produce shallow copies — the lists are separate, but their element references are the same. If the element objects are mutable and you need independent copies, you must deep-copy each element yourself (for example, by calling each element’s copy constructor or clone method and collecting the results):

List<MyType> deep = originalList.stream()
    .map(e -> new MyType(e))   // element-level copy
    .collect(Collectors.toList());

The Object.clone() contract explains that the default cloning is a field-for-field (shallow) copy and that classes must implement Cloneable and override clone() appropriately if they want cloning to work as expected. (docs.oracle.com)

Practical tips: prefer the copy constructor or explicit element-copy methods over clone() for clarity and type safety; if clone() must be used, document why and handle the unchecked cast or suppress the warning deliberately. Also program to the List<E> interface rather than concrete ArrayList<E> when possible. (docs.oracle.com)

The benefit of clone method is to make a local copy of the object you created and this method is inherited from Object class. The class inherit this method can also override it so that it can modify it for its own use. this is the reason it returns an object. Other benefit of returning object that you can cast this returned type to any supper class of the class you use it with;
for example you can cast the cloned ArrayList to list and reuse it as you like and so on.

In your example you are trying to access arrayList method from Object class which doesn't exist.Instead you should cast the value from Object to ArrayList like this:

ArrayList<ObjectMaker> arrayList2 = (ArrayList<ObjectMaker>)theList;

I hope my description is clear.

commented: thanks +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.