You need to sign in to do that
Don't have an account?
S_2013
Deep and shallow clone?
Please help me i am thoroughly confused...
Account a = new Account (Name = 'San Clone');
insert a;
Contact c = new Contact (Lastname = 'Chatterjee', AccountId=a.Id);
insert c;
Contact cdeep = c.clone(false, true);
Contact cshallow = c.clone(false, false);
insert cdeep; insert cshallow;
The code here is creating 3 seperate contact objects but all refering to one single account... so what does this word in Apex documentation mean? "The optional opt_IsDeepClone argument determines whether the method creates a full copy of the sObject field, or just a reference:
- If set totrue, the method creates a full copy of the sObject. All fields on the sObject are duplicated in memory, including relationship fields. Consequently, if you make changes to a field on the cloned sObject, the original sObject is not affected."
"Deep Clone" isn't relevant to your code. It's referring specifically to relationships on an SObject, not individual fields (an ID field is just an individual field, while a relationship is an entire SObject). Some code is order:
An SObject is an object. An object is a specific point in memory, stored at an "address." What the "deep clone" parameter does is tells Apex Code to either copy the memory address (less memory, but changes to any reference changes all of them), or copy the entire object (decouples the reference so they can be used independently, uses more memory).
Your code didn't use an sobject relationship, so there was no extra processing (e.g. true and false would have exhibited the same behavior in your code).
All Answers
"Deep Clone" isn't relevant to your code. It's referring specifically to relationships on an SObject, not individual fields (an ID field is just an individual field, while a relationship is an entire SObject). Some code is order:
An SObject is an object. An object is a specific point in memory, stored at an "address." What the "deep clone" parameter does is tells Apex Code to either copy the memory address (less memory, but changes to any reference changes all of them), or copy the entire object (decouples the reference so they can be used independently, uses more memory).
Your code didn't use an sobject relationship, so there was no extra processing (e.g. true and false would have exhibited the same behavior in your code).
Thanks a lot sdfcfox!