Skip to main content

 In Apex, what happens when you assign one sObject variable to another and modify it? Why?  

 

#Apex

5 respuestas
  1. 4 sept, 05:44

    Hi @Pranjal Budhlakoti

     

    When one sObject variable is assigned to another, both variables refer to the 

    same sObject instance

     in memory. 

     

    For example:

    Account acc1 = new Account(Name = 'Original');Account acc2 = acc1;acc2.Name = 'Updated';System.debug(acc1.Name); // Updated

    Here, changing acc2.Name also changes acc1.Name because both variables reference the same Account object.

    So, assigning an sObject variable does not create a new copy

     of the record; it creates another reference to the same object.  

     

    Hope This Helps!!

0/9000