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

No comments:

HELP !! SHARE !! SUGGEST !!

Powered by Blogger.