Introduction to Apex (Part 2)

In this episode we will pop the hood and dwell further to unravel the mysteries of programmatic development in Salesforce using Apex. Join us as you embark on this wonderful journey to become a Salesforce developer.

Most importantly don’t break a leg if you are overwhelmed with the pace of the live sessions. All Salesforce Developer Training sessions will be recorded and will be available on our YouTube channel. Please Subscribe our YouTube Channel.

Anatomy of a Class

A Class is a user defined datatype (complex-type) and acts as a blueprint for their instances. 

Anatomy of a Apex Class

An Object is an instance of a class i.e. a class in action.

Object is an instance of a apex class

 

Methods in Apex Class

  • Reusable code snippets
  • Specific purpose
  • Knows what comes through
  • Knows what goes out

Demo 1 – Class and Methods

public class Demo11{
 public void printOutput(String stringToDisplay){
    System.debug('Display text: ' + stringToDisplay);
 }
}

Static Vs Instance

Let see what is the difference between Static and instance method in Salesforce.

Static Vs Instance in Apex class
Static Vs Instance

Demo 2 – Static and Instance Variables & Methods

public class Demo2 {
	
	String helloWorldString;
	private static final String DEFAULT_STRING;
    
    static{
        DEFAULT_STRING = 'Hello World';
    }
		
	public Demo2(){
		this(DEFAULT_STRING);
	}
	
	public Demo2(String stringToDisplay){
		this.helloWorldString = stringToDisplay;
	}
	
	public static void printOutput(){
		System.debug('Display text: ' + this.helloWorldString);
	}
}

Demo2 d21 = new Demo2();
d21.printOutput();

Demo2 d22 = new Demo2(‘Jigar’);
d22.printOutput();

Pass by Value Vs Pass by Reference in Apex

In Apex, all primitive data type arguments, such as Integer or String, are passed into methods by value. This means that any changes to the arguments exist only within the scope of the method. When the method returns, the changes to the arguments are lost.

Non-primitive data type arguments, such as sObjects, are also passed into methods by value. This means that when the method returns, the passed-in argument still references the same object as before the method call, and can’t be changed to point to another object. However, the values of the object’s fields can be changed in the method.

Pass by Value Vs Pass by Reference

Demo3 – Pass by Value Vs Reference

public class Demo3 {
    
    public void mainValueMethod(){
        
        String websiteUrl = 'www.apexhours.com';
        
        System.debug('Before value call ' + websiteUrl);
        passByValueCall(websiteUrl);
        System.debug('After value call ' + websiteUrl);
    }
    
    private void passByValueCall(String websiteUrlValue){		//Pass by Value Call
        websiteUrlValue = 'www.salesforce.com';		
    }
    
    public void mainReferenceMethod(){
        
        Account a = new Account();
        a.Name = 'Test Account';
        a.Website = 'www.apexhours.com';
        
        System.debug('Before reference call ' + a);
        passByRefCall(a);
        System.debug('After reference call ' + a);
    }     

    private void passByRefCall(Account a){						//Pass by Reference Call
        a.Website = 'www.salesforce.com';
    }
}
	
	

NOTE: Primitive data types like Integer, Double are pass by value and Objects are pass by reference

Extending a Class

  • Use the virtual keyword
  • Mark the method and class as virtual
  • Extending class can override and provide a new definition to the virtual method

Interfaces

  • A contract that enforces what the child must do
  • Only contains method declarations not definitions
  • Cannot be instantiated on their own

Recording

Agenda

  • Anatomy of Class
  • Methods
  • Static Vs Instance methods
  • Pass by Value Vs Reference
  • Introduction to Object Oriented Programming (OOP)
    • Extending a Class
    • Interfaces
  • Q & A
YouTube video

Further Learning

Apex Hours TrailMixes


Assignment

Complete below assignment to win $1000 Salesforce Voucher. Click here for rule.

Write an Apex class MathCalculator that performs arithmetic operations on Integers based on the following methods on the input numbers passed to them as arguments.
add() – Sums 2 input integer arguments and returns the summation
multiply() – Multiplies 2 input integer arguments and returns the product

Add a method printOutput() that prints the summation or the product from the add or multiple methods to the Debug Log.

(Hint: Implement add(), multiply() and printOutput() as private static methods invoked by a public instance doMath() method. The doMath() method will be invoked from the Execute Anonymous window)

Don’t forget to register for our next session. Check this post for all other session detail.

Please note that we have limit of 500 attendees that can join the online sessions. However, recording will be posted on our YouTube channel. Make sure to subscribe our YouTube channel to get notification for video upload.

So, learn at your pace and free will and ace your journey to Salesforce!

Amit Chaudhary
Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates.

He is a active blogger and founder of Apex Hours.

Articles: 461

60 Comments

  1. Solution :

    public class MathCalculator {

    private static integer add ( integer a, integer b ) {
    return a+b;
    }

    private static integer multiply ( integer a, integer b ) {
    return a*b;
    }

    private static integer subtract ( integer a, integer b ) {
    return a-b;
    }

    public MathCalculator() {
    }

    private static void printOutput ( integer output ) {
    system.debug(output);
    }

    public void doMath ( integer a, integer b, string operator ) {

    integer value = 0;

    if ( operator == ‘+’ . ) {
    value = add ( a , b );
    }

    else if ( operator == ‘*’ ) {
    value = multiply ( a, b ) ;
    }

    else if ( operator == ‘-‘ ) {
    value = subtract ( a, b ) ;
    }

    MathCalculator.printOutput ( value ) ;
    }

    }

    • Hi Arjinder, This is Ankit , I have started with Salesforce recently. Could I request you to Help me with the Above code if you can take out 5 mins from your schedule. I’ll appreciate that!!!

  2. Code for Assignment Day 2
    ——————————–

    public class MathCalculator {
    private static Integer add(Integer a, Integer b){
    return a+b;
    }
    private static Integer multiply(Integer a, Integer b){
    return a*b;
    }

    private static Integer subtract(Integer a, Integer b){
    return a-b;
    }

    private static void printOutput(Integer a, Integer b){
    System.debug(‘Summation of 2 values:’ + add(a,b));
    System.debug(‘Multiplication of 2 values:’ + multiply(a,b));
    System.debug(‘Substraction of 2 values:’ + subtract(a,b));
    }
    public void doMath(Integer a, Integer b){
    printOutput(a, b);
    }

    }

    In Developer Console –> Open Execute Anonymous Window
    ————————————————————————
    MathCalculator mathCalculator = new MathCalculator();
    mathCalculator.printOutput(5,3);

    • Line: 2, Column: 16
      Method does not exist or incorrect signature: void printoutput(Integer, Integer) from the type mathcalculator

      • public class MathCalculator {
        private static integer add(Integer a, Integer b){
        return a+b;
        }

        private static integer multiply(Integer a, Integer b){
        return a*b;
        }

        public static void printOutput(Integer a, Integer b){
        system.debug(‘add’+ add(a,b));
        system.debug(‘multiply:’ + multiply(a,b));
        }

        public static void doMath (Integer a, Integer b){
        printOutput(a,b);
        }
        }

        mathCalculator.printOutput(5,3);

  3. public class MathCalculator {
    private static integer add(integer a,integer b){
    return a+b;
    }
    private static integer multiply(integer a,integer b){
    return a*b;
    }
    private static void printOutput(integer a, integer b){
    System.debug(‘the summation of no is:=’+add(a,b));
    System.debug(‘the Multiplication of no is:=’+multiply(a,b));

    }
    public static void doMath(integer a, integer b){
    printOutput(a,b);
    }

    }

    calling a method:- MathCalculator.doMath(2,3);

  4. Code for Assignment Day 2
    ——————————–

    public class MathCalculator {
    private static Integer add(Integer a, Integer b){
    return a+b;
    }
    private static Integer multiply(Integer a, Integer b){
    return a*b;
    }

    private static Integer subtract(Integer a, Integer b){
    return a-b;
    }

    private static void printOutput(Integer a, Integer b){
    System.debug(‘Summation of 2 values:’ + add(a,b));
    System.debug(‘Multiplication of 2 values:’ + multiply(a,b));
    System.debug(‘Substraction of 2 values:’ + subtract(a,b));
    }
    public void doMath(Integer a, Integer b){
    printOutput(a, b);
    }

    }

    In Developer Console –> Open Execute Anonymous Window
    ————————————————————————
    MathCalculator mathCalculator = new MathCalculator();
    mathCalculator.doMath(8,4);

  5. public class MathCalculator {

    Private static Integer add(Integer num1, Integer num2){
    return num1 + num2;
    }

    Private static Integer multiply(Integer num1, Integer num2){
    return num1 * num2;
    }

    Private static void printOutput(Integer result){
    System.debug(‘Result: ‘ + result);
    }

    Public void doMath(String operation, Integer num1, Integer num2){
    if(operation == ‘Add’)
    MathCalculator.printOutput(MathCalculator.add(num1, num2));
    else if(operation == ‘Multiply’)
    MathCalculator.printOutput(MathCalculator.multiply(num1, num2));
    }
    }
    /*
    MathCalculator mc = new MathCalculator();
    mc.doMath(‘Add’, 10, 15);
    mc.doMath(‘Multiply’, 4, 20);
    */

  6. public class MathCalculator{
    private static Integer multiply(Integer num1, Integer num2){
    return(num1*num2);
    }
    private static Integer add(Integer num1, Integer num2){
    return(num1+num2);
    }
    private static void printOutput(Integer result){
    system.debug(‘Result: ‘+result);
    }
    public void doMath(String operation, Integer num1, Integer num2){

    if(operation==’Add’)
    MathCalculator.printOutput(MathCalculator.add(num1,num2)); //aggregation concept

    else if(operation==’Multiply’)
    MathCalculator.printOutput(MathCalculator.multiply(num1,num2)); //aggregation concept
    else
    System.debug(‘Invalid Operation’);
    }

    }

  7. public class Mathcalculater {
    private static integer add(integer sum1,integer sum2){
    return sum1+sum2;

    }
    private static integer multiply(integer sum1,integer sum2){
    return sum1*sum2;
    }
    private static void printOutput(integer sum1,integer sum2){
    system.debug(‘summation value is…’+ add(sum1,sum2));
    system.debug(‘product value is…’+ multiply(sum1,sum2));
    }
    public void doMath(integer sum1, integer sum2){
    printOutput(sum1,sum2);
    }
    }
    ………………………………………Execute window
    Mathcalculater MC=new Mathcalculater();
    MC.doMath(5,6);

  8. /* Write an Apex class MathCalculator that performs arithmetic operations on Integers based on the following methods on the input numbers passed to them as arguments.
    •add() – Sums 2 input integer arguments and returns the summation
    •multiply() – Multiplies 2 input integer arguments and returns the product

    Add a method printOutput() that prints the summation or the product from the add or multiple methods to the Debug Log.

    (Hint: Implement add(), multiply() and printOutput() as private static methods invoked by a public instance doMath() method. The doMath() method will be invoked from the Execute Anonymous window) */

    public class MathCalculator // Apex class mathcalculator
    {
    private static integer add (Integer Num1, integer num2)
    { // Private static method => Sums of 2 inputs input integer arguements and return the summation.
    return Num1+num2;
    }
    private static integer multiply ( integer Num1 , integer num2)
    {
    return Num1*num2; // Private static method =Multiples 2 inputs integer adguements and multiplication.
    }
    private static void printout(integer Num1, integer num2)
    { // Private static method= Display addition and substraction.
    system.debug(‘Addition of two values=>’+add(Num1,num2));
    system.debug(‘Multiplication of two values=>’+multiply(Num1,num2));
    }
    public void domath(integer Num1, integer num2) // public instance method to perform domath…
    {
    printout(Num1,num2);
    }
    }

    /********** Execute anonymous window*************

    MathCalculator Mathcal = new MathCalculator();
    Mathcal.domath(25,2);

    *********** Out Put **************************

    Addition of two values=> 27
    Multiplication of two values=> 50

    ************************************************/

  9. public class MathCalculator {
    private static Integer add(Integer a, Integer b)
    {
    return a+b;
    }
    private static Integer multiply(Integer a, Integer b)
    {
    return a*b;
    }
    private static void printOutput(Integer a, Integer b)
    {
    System.debug(‘Sum:’ + add(a,b));
    System.debug(‘Multiplication:’ + multiply(a,b));
    }
    public void doMath(Integer a, Integer b)
    {
    printOutput(a, b);
    }

    }

    Execute Anonymous window
    MathCalculator mathCalculator= new MathCalculator();
    mathCalculator.doMath(3,5);

  10. Atleast give full codes in your information.
    the virtual keyword code as well as the interface codes are not present.

  11. public class MathCalculator {

    private static Integer add(integer a, integer b) {
    return a+b;
    }
    private static Integer multiply(integer a, integer b) {
    return a*b;
    }
    private static Integer subtract(integer a, integer b) {
    integer c;
    c = (a > b) ? a – b : b – a;
    return c;
    }

    private static void printOutput(integer a,integer b,String operation) {
    integer result;
    if(operation == ‘add’) { result = add(a,b); }
    else if (operation == ‘multiply’) { result = multiply(a,b);}
    else { result = subtract(a,b); }

    system.debug(‘Result of required operation is: ‘ + result);
    }

    public void doMath(Integer a,Integer b,String operation) {
    printOutput(a,b,operation);
    }
    }

    @ Execute Anonymous Window:
    MathCalculator mathCalc = new MathCalculator();
    mathCalc.printOutput(5,9,’subtract’);

    Output:
    >>> Result of the required operation is: 4

  12. Class
    =====
    public class MathCalculator {

    private static Integer add(Integer a, Integer b)
    {
    return a+b;
    }

    private static Integer multiply(Integer a, Integer b)
    {
    return a*b;
    }

    private static void printOutput(Integer result)
    {
    System.debug(‘The result of operation is: ‘+result);
    }

    public void doMath(String operation, Integer a, Integer b)
    {
    if(operation == ‘add’)
    {
    Integer result = add(a,b);
    printOutput(result);
    }

    else if(operation == ‘multiply’)
    {
    Integer result = multiply(a,b);
    printOutput(result);
    }
    else
    {
    System.debug(‘Enter “add” or “multiply” as input!’);
    }
    }
    }

    In Execute Anonymous window:
    ==========================
    MathCalculator mc = new MathCalculator();
    mc.doMath(‘add’,3,4);

  13. Public Class MathCalculator {
    private static Integer add(Integer a, Integer b){
    return a+b;
    }
    private static Integer multiply(Integer a, Integer b){
    return a*b;
    }
    private static void printOutput(Integer a, Integer b){
    System.debug(‘The Sum of 2 values: ‘ + add(a,b));
    System.debug(‘The product of 2 values: ‘ + multiply(a,b));
    }

    public static void doMath(Integer a, Integer b){
    printOutput(a, b);
    }}

    Anonymous window:
    ==========================
    MathCalculator.doMath(10,2);

  14. Apex does not have a virtual keyword for classes or methods. In Apex, classes are implicitly extendable, meaning you can create a base class, and other classes can extend it without using any specific keyword. Correct me if I am wrong.

  15. public class MathCalculator {
    //Add two integers
    private static Integer add(Integer a1,Integer a2){
    return a1 + a2;
    }

    //Multiply two integers
    private static Integer multiply(Integer m1,Integer m2){
    return m1 * m2;
    }

    //PrintOutput
    private static void printOutput(Integer a1,Integer a2){
    System.debug(‘Addition of two integers:’+ add(a1,a2));
    System.debug(‘Multiplication of two integers:’+ multiply(a1,a2));
    }

    public static void doMath(Integer a1,Integer a2){
    printOutput(a1,a2);
    }
    }

  16. public class Math {

    private static Integer add(Integer a, Integer b){
    return a+b;
    }
    private static Integer multiply(Integer a, Integer b){
    return a*b;
    }
    private static void printOutput(Integer a, Integer b){
    System.debug(‘result for addition:’+ add(a,b));
    System.debug(‘result for Multipilication:’+ multiply(a,b));
    }
    public void doMath(Integer a, Integer b){
    printOutput(a,b);
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *