#How do you deep clone an Object class that's a constant?

1 messages · Page 1 of 1 (latest)

wispy siloBOT
#

This post has been reserved for your question.

Hey @severe fractal! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

solid bough
#

Just don't. Make a copy constructor or something of the sort instead. Java's clone() mechanism isn't as serious as it is made to sound like. It refuses to handle deep clones, and that's not a problem as you don't particularly need this mechanism

severe fractal
spare oriole
#

Implement Cloneable on the final object so you can just clone it, if by copy you mean actually copy

solid bough
#

I don't think you have instructions that require you circumvent Java's final restrictions

severe fractal
#

I have a pretty difficult instructor lol

solid bough
#

I'll believe it when.I see it, but anyway, need to go to bed

severe fractal
#

Here's your proof buddy.

spare oriole
#

If you’re copying or cloning then I don’t see why you need to modify a final

severe fractal
#

He specifically tells us to mark SecurityCredentials as a constant.
Then tells us to deepclone it

spare oriole
#

You can still read a final object

#

You can’t write to it

severe fractal
#

then what part of my code is incorrect?

spare oriole
#

If you’re doing a deep copy, you’re going into every single field and copying it, including the ones inside of fields

severe fractal
#

right..

#

So, when i do..

cloned.secCred = new SecurityCredentials(secCred);

It doesn't allow me to do so because I am trying to change secCred.

#

But thats what the instructions state to do.

solid bough
#

I'd assume that final fields are immutable and don't need to be copied even when doing a deep copy

severe fractal
#

what do you think my instructor wants us to clone?

#

He doesn't specify much unfortunately. Just "Clone() - return a deep clone"

spare oriole
#

When you clone an object it copies the fields

#

Even the finals

#

So when you are cloning, you don't have to revisit every field

#

Of course, you should still revisit some things but not entirely reassign them, otherwise I'd suggest a private ctor

#

Take a look at this: ```java
public class Solution {

public static void main(String[] args) throws CloneNotSupportedException {
    var rect = new Rectangle(9, 10);
    var rect2 = rect.clone();
    System.out.println(rect2);
}

}

class Rectangle implements Cloneable {
final int width;
final int height;

public Rectangle(int width, int height) {
    this.width = width;
    this.height = height;
}

@Override
protected Object clone() throws CloneNotSupportedException {
    return super.clone();
}

@Override
public String toString() {
    return "Rectangle{" +
            "width=" + width +
            ", height=" + height +
            '}';
}

}```

severe fractal
#

ctor?

spare oriole
#

Cloning the rectangle will get me another rectangle that is 9x10

severe fractal
#

You're code is a shallow clone though

spare oriole
#

I see

#
@Override
    protected Object clone() {
        return new Rectangle(width, height, intList.stream().toList());
    }```
#

That returns a new rectangle and a new list

#

Comparing the rectangles return false, comparing the lists return false

#

oh I forgot to show the class now ```java
class Rectangle implements Cloneable {
final int width;
final int height;
final List<Integer> intList;

public Rectangle(int width, int height, List<Integer> intList) {
    this.width = width;
    this.height = height;
    this.intList = intList;
}

@Override
protected Object clone() {
    return new Rectangle(width, height, intList.stream().toList());
}

@Override
public String toString() {
    return "Rectangle{" +
            "width=" + width +
            ", height=" + height +
            ", intList=" + intList +
            '}';
}

}```

#

It contains an list where the intList would be equal references with just a shallow

#

You don't necessarily have to internally set the field, you can do it using copy constructors

#

You can call clone on SecurityCredentials if implemented and use that new clone with a private constructor

spare oriole
#

Or... you know... java var field = rect2.getClass().getDeclaredField("intList"); field.setAccessible(true); field.set(rect2, rect.intList);

#

Reflect it