Setter and Getter In Salesforce

0
2585

When we want to refer to Apex class variables in the visualforce page we need to use Apex getter and setter methods. In this Salesforce tutorial, we will understand about Apex getter method and setter method in detail.

Getter Method: Used to read the value stored in the variable and display it in the VisualForce Page.


Setter Method: Used to read the value from VF Page and store data to the variable in the apex class.


, , , ,

Example With Code sample

Public class BasicClass {

   // Property declaration
   access_modifier return_type property_name {
      get {
         //Get accessor code block
      }
      set {
         //Set accessor code block
      }
   } 
}

Where:

  • access_modifier is the access modifier for the property. The access modifiers that can be applied to properties include: publicprivateglobal, and protected. In addition, these definition modifiers can be applied: static and transient. For more information on access modifiers, see Access Modifiers.
  • return_type is the type of the property, such as Integer, Double, sObject, and so on. For more information, see Data Types.
  • property_name is the name of the property

For example, the following class defines a property named prop. The property is public. The property returns an integer data type.

public class BasicProperty {
   public integer prop {
      get { return prop; }
      set { prop = value; }
   }
}

The following code segment calls the BasicProperty class, exercising the get and set accessors:

BasicProperty bp = new BasicProperty();
bp.prop = 5;                   // Calls set accessor
System.assertEquals(5, bp.prop);   // Calls get accessor

Note the following:

  • The body of the get accessor is similar to that of a method. It must return a value of the property type. Executing the get accessor is the same as reading the value of the variable.
  • The get accessor must end in a return statement.
  • We recommend that your get accessor not change the state of the object that it is defined on.
  • The set accessor is similar to a method whose return type is void.
  • When you assign a value to the property, the set accessor is invoked with an argument that provides the new value.
  • When the set accessor is invoked, the system passes an implicit argument to the setter called value of the same data type as the property.
  • Properties cannot be defined on interface.
  • Apex properties are based on their counterparts in C#, with the following differences:
    • Properties provide storage for values directly. You do not need to create supporting members for storing values.
    • It is possible to create automatic properties in Apex. For more information, see Using Automatic Properties.

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