Lets first separate it out and see what each one means.
What is Shallow Copy?
A Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, just the reference addresses are copied, i.e., only the memory address is copied.
In this figure, the MainObject1 have fields “field1” of int type, and “ContainObject1” of ContainObject type. When you make a shallow copy of MainObject1, MainObject2 is created with “field3” containing the copied value of “field1” and still pointing to ContainObject1 itself. Observe here and you will find that since field1 is of primitive type, the values of it are copied to field3 but ContainedObject1 is an object, so MainObject2 is still pointing to ContainObject1. So any changes made to ContainObject1 in MainObject1 will reflect in MainObject2.
What is Deep Copy?
A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. A deep copy occurs when an object is copied along with the objects to which it refers.
In this figure, the MainObject1 have fields “field1” of int type, and “ContainObject1” of ContainObject type. When you do a deep copy of MainObject1, MainObject2 is created with “field3” containing the copied value of “field1” and “ContainObject2” containing the copied value of ContainObject1.So any changes made to ContainObject1 in MainObject1 will not reflect in MainObject2.
Let’s get into deep by differentiating them further :
Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.
Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated.
Account acc1 = new account ();
Account acc2 = new account ();
Frequently Asked Salesforce Interview Questions & Answers
Shallow copying, means acc2 is handled for acc1 but the values of acc2 are not referred in acc1, only acc2 is acting as handle for the object acc1
Here the values of acc2 are transferred to acc1
Global class counter {
Integer NUM = 0;
Public counter () {
Num ++;
}
Public void get count (){
System debug (‘ the count is ‘ +num);
}
}
Global class test {
Public static test method void main (){
Counter c1= new counter ();
Counter c2 = new counter ();
c3. Get count();
}
}
No. of objects created is 1
Actually, variable are three types
1.Local variables → belongs to the method
2.Object variables → belongs to object
3.Class variables → belongs to a class
4.A class variable belongs to the class only not to the other object
→ CV existence is independence of objects
Eg:
Global class counter {
Private integer num; 11 object variable
Private static integer counter = 0; 11 class variable
Public counter (){
Num = 0;
Count ++;
}
Public counter (integer num ){
This. Num = num
Count ++;
}
Public void static value (integer num ){
This. Num = num;
}
Public void get values (){
System. Debug (‘num = ‘ + num);
}
Public void get count (){
System. Debug (‘count of objects = ‘+count);
}
}
Global class test {
Public static test method void main (){
Counter c1 = new counter ();
Counter c2 = new counter ();
Counter c3 -= new counter (10);
c1. Get values ();
}
}
No. of objects 3
We can call the same methods through
Class name. method name
Eg: counter. Get count ();
Public static void get count() {
System.debug c ‘count of objects =’+ count);
}
We can call the static methods, without caption of the object
Object variables count accesses through static method,
But we can access static variables only
Eg: public static void get count (){
//static var à system debug (‘count of objects =’ + count);
// object var à system debug (‘num =+num);
}
Check Out Salesforce Tutorials
Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
Name | Dates | |
---|---|---|
Salesforce Training | Nov 23 to Dec 08 | View Details |
Salesforce Training | Nov 26 to Dec 11 | View Details |
Salesforce Training | Nov 30 to Dec 15 | View Details |
Salesforce Training | Dec 03 to Dec 18 | View Details |
Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.