#How do you deep clone an Object class that's a constant?
1 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @severe fractal! Please use
/closeor theClose Postbutton 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.
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
We have too bro. Its part of the instructions lol
Implement Cloneable on the final object so you can just clone it, if by copy you mean actually copy
I don't think you have instructions that require you circumvent Java's final restrictions
did that
sorry, but i do.
I have a pretty difficult instructor lol
I'll believe it when.I see it, but anyway, need to go to bed
If you’re copying or cloning then I don’t see why you need to modify a final
He specifically tells us to mark SecurityCredentials as a constant.
Then tells us to deepclone it
what do u mean?
then what part of my code is incorrect?
If you’re doing a deep copy, you’re going into every single field and copying it, including the ones inside of fields
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.
I'd assume that final fields are immutable and don't need to be copied even when doing a deep copy
what do you think my instructor wants us to clone?
He doesn't specify much unfortunately. Just "Clone() - return a deep clone"
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 +
'}';
}
}```
ctor?
Cloning the rectangle will get me another rectangle that is 9x10
You're code is a shallow clone though
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