Hey guys, Welcome back to the blog, Let’s quickly look into Constructors & Methods In Salesforce.
Constructor
This is a special method defined with the same class name.
1. It is used to initialize the data members of the class.
2. It will not have any return type.
3. Constructor will invoke only one at the time of creating an object.
Class Methods
To define a method, specify the following:
- Optional: Modifiers, such as public or protected.
- Required: The data type of the value returned by the method, such as String or Integer. Use void if the method does not return a value.
- Required: A list of input parameters for the method, separated by commas, each preceded by its data type, and enclosed in parentheses (). If there are no parameters, use a set of empty parentheses. A method can only have 32 input parameters.
- Required: The body of the method, enclosed in braces {}. All the code for the method, including any local variable declarations, is contained here.
Use the following syntax when defining a method:
[public | private | protected | global] [override] [static] data_type method_name
(input parameters)
{
// The body of the method
}
Let’s Understands these with Few Examples
Example of Constructor: Display data in Visualforce Page
Apex Class:
public class Employee {
public String name {set;get;}
public String branch {set;get;}
public String city {Set;get;}
public String designation {Set;get;}
public Employee(){
name = 'Salesforce Cody';
branch = 'R T Nagar';
city = 'Bangalore';
designation = 'Senior Salesforce Devloper';
}
}
VisualForce Page:
<apex:page controller="Employee">
Name :{!name }<br/><br/>
Branch:{!branch}<br/><br/>
City : {! city } <br/><br/>
Designation : {! designation}
</apex:page>
That’s the Nice output!!
So this is about Constructor and its a small example, Let’s Jump into Salesforce Methods
Example of Method
Apex class:
public class calculater {
public integer first {set;get;}
public integer second {set;get;}
public integer total {set;get;}
public void add(){
total=first+second;
}
public void subtract(){
total=first-second;
}
public void multiply(){
total=first*second;
}
public void reset(){
total=null; first=null; second=null;
}
}
VisualForce Page
<apex:page controller="calculater" >
<apex:form>
<apex:pageblock Title="Sample Calculator" id="pb" >
<apex:pageBlockSection columns="1" >
<apex:pageBlockSectionItem>
<apex:outputLabel> First Value</apex:outputLabel> <apex:inputtext value="{!first}"></apex:inputtext>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem>
<apex:outputLabel > Second Value</apex:outputLabel> <apex:inputtext value="{!second}"></apex:inputtext>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem>
<apex:outputLabel> Total</apex:outputLabel> <apex:inputtext value="{!total}"></apex:inputtext>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageblock>
<apex:commandbutton value="Add" action="{!add}" rerender="pb"/>
<apex:commandbutton value="Subtract" action="{!subtract}" rerender="pb"/>
<apex:commandbutton value="Multiplication" action="{!multiply}" rerender="pb"/>
<apex:commandbutton value="Refresh" action="{!reset}" rerender="pb"/>
</apex:form>
</apex:page>
Let’s see output:
that’s all for this blog, check other blogs here.
Affordable Ways to Boost Your Resume Affordable Ways to Demonstrate Your Salesforce Knowledge Affordable Ways to Enhance Your Career Affordable Ways to Get Certified Affordable Ways to Validate Your Salesforce Skills Apex Buy Your Certification Vouchers Today Discounted Certification Exam Vouchers Discounted Exam Vouchers Discounted Exam Vouchers for Salesforce Certifications Discounted Salesforce Certification Exams Discounted Vouchers for Salesforce Certifications Get Certified and Boost Your Career Get Certified and Boost Your Earnings Get Certified and Get Ahead Get Certified and Stand Out from the Crowd Get Certified at a Discount Get Certified at a Discounted Price Invest in Your Career with Certification Invest in Your Future with Salesforce Certification Maximize Your Salesforce Career Potential Saleforce crm Salesforce Salesforce Certification salesforce certification coupon codes Salesforce Certification Preparation salesforce certification vouchers SalesforceCody Salesforce Cody Salesforce Exam Salesforce Interview Salesforce Interview Questions Salesforce Trigger Salesforce Trigger Advance Salesforce Trigger Basics Salesforce Trigger Example Salesforce Triggers Tasks Salesforce Trigger Tasks Salesforce Updates Save Money and Get Certified Save Money on Certification Exams Save Money on Salesforce Certification Exams Save Money on Your Exams Trigger Trigger Tasks Apex
- Top Salesforce Flow Interview Questions & Answers 2024 Part – 3
- Top Salesforce Flow Interview Questions & Answers 2024 Part – 2
- Best Practices for Lightning Web Components (LWC)
- Top Salesforce Flow Interview Questions & Answers 2024 Part – 1
- Mastering Salesforce Flows: Streamline Your Business Processes with Ease