Wednesday 23 September 2015

Final static variables can only be assigned in their declaration or in a static block salesforce

       You cannot assign the final static variable value other than the declaration of variable or static block. If you try to assign it, then you will get an error as "Final static variables can only be assigned in their declaration or in a static block"

To solve this issue, you need to assign like below,

  1. public class StaticFinalController
  2. {
  3.     // assign value for get property variable using static block.
  4.     public static final string staticFinalVariable{get;}
  5.     static
  6.     {
  7.         staticFinalVariable = 'Test Value';
  8.     }
  9.     // Assign value on declaration
  10.     public static final string staticFinalVariable1 = 'Test';
  11.  
  12.     public StaticFinalController()
  13.     {
  14.       system.debug('### Final Value is:::::'+staticFinalVariable );
  15.     }
  16.  
  17. }


Note: Static code blocks run before the constructor.

Reference:

Static and Instance Methods, Variables, and Initialization Code

No comments:

Post a Comment

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...