APEX : CONSTRUCTOR
Constructor
The constructor is a special method which has the following properties :
For Example :
- 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()
{
}
=================================================
=================================================
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.
=================================================
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.
=================================================
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.
=================================================
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 >>
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
Reviewed by
on
Rating:
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.
ReplyDeleteNice Question...We can use render(){} function in LWC.
DeleteThis will help you to achieve this.
I hope this will help you.
⚡️HAPPY LEARNING⚡️
u mean to call the apex class constructor from LWC JS render function.I'll let u know if it works thanks!.
Deletehow to import apex constructor in lwc
ReplyDeleteHey!! In LWC we use following syntax to import Methods in JavaScript file as :
Deleteimport apexMethodName from '@salesforce/apex/Namespace.Classname.apexMethodReference';
I hope the above syntax will help you!!
⚡️HAPPY LEARNING ⚡️
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?
ReplyDeleteIts only calling contractor to display the value inside the constructor.
DeleteHey 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.
ReplyDeleteThanks !!!
Please exaplain with example how to wtite test class for constructor
ReplyDelete@Ajinkya Dhas ,Its very Helpful and easy to understand specially this Line
ReplyDelete"Usage of Apex Program within Visualforce Page > 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."
got exact idea what happens behind the scene
Thank you so much
really good website to learn Salesforce
Thanks Akshay. I am glad that this platform is helping your to learn new things in your career.
DeleteHappy Learning ☁️⚡️
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. Visit Saudi Arabia
ReplyDelete