APEX : CONSTRUCTOR

salesforce apex constructor by salesforcekid

Constructor

The constructor is a special method which has the following properties :
  • Method name will be the same as a class.
  • Access specifier will be public.
  • This method will be invoked only once that is at the time of creating an object.
  • This is used to instantiate the data members of the class.

For Example :
=================================================
public class TestObject
 {
   \\The no argument constructor

    public TestObject()
     {
       \\code
      }
  }
=================================================

There are 3 types of constructors:
  • 1. Default Constructor
  • 2. Non-parameterized Constructor
  • 3. Parameterized Constructor 

1. Default Constructor

If an Apex Class doesn't contain any constructor then Apex compiler by default creates a dummy constructor on the name of the class when we create an object for the class.

For Example:
=================================================
public class Example
 {
  
  }
 Example e = new Example();

=================================================

In the above example, the apex class doesn't contain any constructor. So when we create an object for example class the Apex compiler creates a default constructor.

For Example:
=================================================
public example()
{

}
=================================================

2. Non-parameterized Constructor

  • It is a constructor that doesn't have any parameters is called Non-parameterized constructor.
  • Parameters are nothing but the values we are passing inside a constructor.
For Example:
=================================================
public class Example
{
  Integer number;
   String Name;

   public Example()  //Non-parametarized Constructor
   {
    number=10;
     name=Salesforcekid;
   }

}
=================================================

3. Parameterized Constructor 

  • It is a constructor that has parameters.
  • That means here we will take input from the user and then map it with our variable.
For Example:
=================================================
public class Example
 {
   Integer number;
   String Name;

public Example(Integer x, String myname) //Parametarized Constructor
 {
   number = x;
   Name = myname;
 }
=================================================

Now let's create an apex program to understand the use of constructor in an apex.
  • Open developer console by clicking the org name on the Salesforce page.
  • Click File --> New --> Apex Class.
  • Enter the class name.
  • Write the Apex Class.
For Example:
=================================================
public class Employee //Class
 {
   String EmployeeName;
   Integer EmployeeNo;
  
  public Employee() //Constructor
  {
    EmployeeName = 'Ajinkya';
    EmployeeNo = 10;
   }
 public void show() //Method
 {
   System.debug('Employee Name is '+ EmployeeName);
   System.debug('Employee No is '+ EmployeeNo);
  }

}
=================================================

Now open anonymous window and type this 
=================================================
Employee c1 = new Employee();
Employee c2 = new Employee();

c1.show();
c2.show();
=================================================

This will give you the following output
=================================================
EmployeeName is Ajinkya
EmployeeNo is 10
=================================================

Usage of Apex Program within Visualforce Page

When you want to call Apex class in visualforce page we have to declare our apex class in the following format.
=================================================
<apex:page Controller="ClassName">
=================================================

Whenever we call a visualforce page in which controller attribute is defined it will first create an object for the apex class which is defined in the controller.

You can call it a client-side controller.

When an object is created for the Apex class first it invokes the constructor.



WOHOOO.....You have just completed this Apex: Constructor episode.
If you like this salesforcekid learning platform please let me know in the Comment section...Also, Share with your salesforce folks wish you 
Happy learning ☁️⚡️ (Learn. Help. Share.)

<< PREVIOUS                                                      NEXT >>
APEX : CONSTRUCTOR APEX : CONSTRUCTOR Reviewed by on Rating: 5

9 comments:

  1. Like u mentioned when we call a Visualforce page it will calls the constructor how to do the same when using Lightning Web components instead of Visualforce.

    ReplyDelete
    Replies
    1. Nice Question...We can use render(){} function in LWC.

      This will help you to achieve this.

      I hope this will help you.

      ⚡️HAPPY LEARNING⚡️

      Delete
    2. u mean to call the apex class constructor from LWC JS render function.I'll let u know if it works thanks!.

      Delete
  2. Anonymous10/01/2020

    how to import apex constructor in lwc

    ReplyDelete
    Replies
    1. Hey!! In LWC we use following syntax to import Methods in JavaScript file as :

      import apexMethodName from '@salesforce/apex/Namespace.Classname.apexMethodReference';

      I hope the above syntax will help you!!

      ⚡️HAPPY LEARNING ⚡️

      Delete
  3. What exactly is going on with the c1.show(); command? I'm going to botch this syntax, but is it something like "show(Employee c1);" where the c1 is an input into the show method?

    ReplyDelete
    Replies
    1. Its only calling contractor to display the value inside the constructor.

      Delete
  4. Hey i am new to programming just want to ask why we need constructor? and what exactly it do in apex? real life scenario example will be better for understanding.
    Thanks !!!

    ReplyDelete
  5. It is a common fact that we find a wide variety of construction machines on every construction sites, which make the construction jobs easy, safe and quicker. Depending on the application, construction machines are classified into various categories which we are discussing here. شرکت آرین سازه

    ReplyDelete

HELP !! SHARE !! SUGGEST !!

Powered by Blogger.