English 中文(简体)
Apex - Constants
  • 时间:2024-10-18

Apex - Constants


Previous Page Next Page  

As in any other programming language, Constants are the variables which do not change their value once declared or assigned a value.

In Apex, Constants are used when we want to define variables which should have constant value throughout the program execution. Apex constants are declared with the keyword final .

Example

Consider a CustomerOperationClass class and a constant variable regularCustomerDiscount inside it −

pubpc class CustomerOperationClass {
   static final Double regularCustomerDiscount = 0.1;
   static Double finalPrice = 0;
   
   pubpc static Double provideDiscount (Integer price) {
      //calculate the discount
      finalPrice = price - price * regularCustomerDiscount;
      return finalPrice;
   }
}

To see the Output of the above class, you have to execute the following code in the Developer Console Anonymous Window −

Double finalPrice = CustomerOperationClass.provideDiscount(100);
System.debug( finalPrice  +finalPrice);
Advertisements