Table of Contents
Previous Sessions
If you missed the previous sessions, please use the below links for the previous sessions
- https://www.pantherschools.com/methods-in-salesforce-in-hindi/
- https://www.pantherschools.com/introduction-to-oops-using-salesforce
- https://www.pantherschools.com/data-types-class-access-modifiers-in-salesforce
Constructor in Salesforce
A constructor is a unique method that is used to create and initialize an instance/Object of a class. When an object is created from a class, the constructor is automatically called to initialize the object’s state and properties.
- The constructor does not have any return type.
- The name of the constructor must be the same as the Class Name.
- The parent class(topmost class) constructor should always be public.
- Private in cases where the methods are class methods.
Types of Constructor
There are two types of constructors in Salesforce:
- Default Constructor: A default constructor is a constructor that is automatically created by Salesforce if no other constructor is defined in the class.
- The default constructor takes no arguments and does not perform any initialization.
- A parameterized constructor is a constructor that takes one or more parameters and uses them to initialize the object’s state and properties.
- When you want to enforce that user must need to pass the value while creating the instance/Object.
- Parameterized constructors are useful for setting initial values or default values for object properties.
- Animal dog = new Animal(‘Paremrian’);
Conditional Statements
- Conditional Statements
- If else
- Switch
Use the “if-else” statement in Apex code to execute different blocks of code based on a condition
Switch Statement in Salesforce
You can use the “switch” statement in Apex code to execute different blocks of code based on the value of a variable.
The syntax for the “switch” statement is as follows:
Here’s an example of how to use the “switch” statement in Apex code:
Hands-on Code
Assignment
Create a class “Car” that should have the following
- The class should have the private variables
- Name
- Model
- Year
- Company
- Type
- Create multiple constructors that are accepting the different parameters
- Car(String name, String type)
- Car(String name)
- Car( )
- Car(String name, String model, String year, String company, Type)
- Car(Integer year)
- Within the constructors assign the values to the private variables. Note: – The value should be assigned to the variable that is passed in the constructor and for other variables assign a blank value. See the example below
- public Car(String year){
this.year = year;
this.name = ”;
this.model = ”;
this.company = ”;
this.type = ”;
}
- public Car(String year){
- Create various methods to print the Car Details
- printCarInformation( ) → This method should print all the information about the Car
- printCarName( ) → This method should only print the name of the Car
- printCarYear( ) → This method should only print the built year of the Car
- printCarNameType( ) → This method should only print the name & type of the Car
Full Video
Resources
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_constructors.htm
- https://www.tutorialkart.com/learn_apex/constructor-in-apex-programming/
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_if_else.htm
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_switch.htm
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_expressions_operators_understanding.htm
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SafeNavigationOperator.htm
- Math Class – https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_math.htm
- String Class – https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm
