ApexGuru OR ApexLearner Quiz 2
Unpredictability means what it means. I don't know how you define it. It is what it is. (Michael Keaton)
But don't worry if your fundamentals are strong in Apex programming you can predict the output for almost any of the code :)
ApexGuru Apex class code as mentioned below.
public class ApexGuru {
public void methodOne(){
Map<Id,Contact> apexGuruMap = new Map<Id,Contact>();
methodTwo(apexGuruMap);
System.debug('Apex Guru has won with count :::'+apexGuruMap.size());
}
public void methodTwo(Map<Id,Contact> apexGuruMap){
apexGuruMap = new Map<ID, Contact>([SELECT Id, LastName FROM Contact LIMIT 3]);
}
}
ApexLearner Apex class code as mentioned below.
public class ApexLearner {
public void methodOne(){
Map<Id,Contact> apexLearnerMap = new Map<Id,Contact>();
methodTwo(apexLearnerMap);
System.debug('Apex Learner has won with count :::'+ apexLearnerMap.size());
}
public void methodTwo(Map<Id,Contact> apexLearnerMap){
Map<Id,Contact> apexLearnerMapTemp = new Map<ID, Contact>([SELECT Id, LastName FROM Contact LIMIT 3]);
apexLearnerMap.putAll(apexLearnerMapTemp);
}
}
Let me know the output in the comment section if we execute below syntax through Anonymous window.
ApexGuru aGuru = new ApexGuru();
aGuru.methodOne();
------------------------------
ApexLearner aLearner = new ApexLearner();
aLearner.methodOne();
?
*Note: Please don't waste your time if you run the same in Developer console and paste the output here as this way we are not going to make any changes in our development skills.
If you like this article please share with other Salesforce programmers as well.
Certified Salesforce Application Architect | 11 X Salesforce Certified | Delivery Hero
5 年Apex Guru has won with count :::0 Apex Learner has won with count :::0
Technical Lead | 4x Certified Salesforce Developer | APEX | Visualforce | JavaScript | Aura | LWC | Integration
7 年Apex Guru has won with count :::0 Apex Learner has won with count :::3 When ApexGuru class in instantiated and methodOne is called, apexGuruMap Map variable is local to methodOne and exist on stack frame of methodOne in stack Memory.In methodTwo call, we are initializing the map variable, that means, the variable is local to methodTwo. Where as in ApexLearner, a new map variable is created and its value is Copied to the apexGuruMap. So in output the size of the map is 3.
Director of Salesforce Architecture @ Cint | Designing Innovative Salesforce Solutions
7 年For both the controllers the output will be only debug statement with count 0. Apex Guru has won with count :::0 Apex Learner has won with count :::0