13 votes

Is there any benefit, performance-wise, to declaring Apex variables as final?

Too long for a comment, not a proper evaluation for 100% confidence So if you run the following script in anonymous Apex you will find that the loop with the final variable always runs faster than ...
zgc7009's user avatar
  • 299
13 votes

Is there any benefit, performance-wise, to declaring Apex variables as final?

I only use it as part of making the intent clearer in code. So to me: private static final String OPEN = 'Open'; or: private static final Set<DisplayType> LIKE_SUPPORTED = new Set<...
Keith C's user avatar
  • 135k
8 votes

What is this technique called?

It's called Lazy Loading or Lazy initialization Lazy loading is a design pattern to defer initialization until the point at which it is needed. Reference Lazy Loading Lazy initialization
Robs's user avatar
  • 9,187
7 votes

Is there any benefit, performance-wise, to declaring Apex variables as final?

There's a benefit for you, the developer, since you're restricting the possible ways a variable can be assigned. For example, you're more likely to catch accidental assignments (using = instead of ==),...
sfdcfox's user avatar
  • 482k
7 votes

Is there any benefit, performance-wise, to declaring Apex variables as final?

One big optimization regardless of what the compiler does with the declaration is that you can eliminate many null checks from your code. Consider the below example. If you remove the final ...
Adrian Larson's user avatar
  • 149k
7 votes

Dynamic Fields in for Loop

Yes you just use get instead. You can cache a String or SObjectField. SObjectField field = Account.ParentId; for (Account record : [SELECT ParentId FROM Account LIMIT 10]) { Object value = record....
Adrian Larson's user avatar
  • 149k
5 votes
Accepted

Round off an apex variable upto two decimal places

Keeping everything in visualforce here, there is a function to do precisely what you're looking for, ROUND. It appears about half-way down the documentation page on visualforce functions ROUND ...
Derek F's user avatar
  • 59.3k
5 votes

Variables in Interface in Salesforce

Can you declare variables in Interface in Apex (Salesforce). No. Also in Apex, are all abstract methods are public by default? No. All methods and classes are private by default. You must provide ...
sfdcfox's user avatar
  • 482k
4 votes
Accepted

Get value from custom setting

Change Integer oppsize to static as that is being accessed from static variable. static Integer oppsize = setting.Batch_Size__c; private static final Integer MAX_OPPTLI_IN_BATCH = oppsize;
Santanu Boral's user avatar
4 votes
Accepted

Compile Error on Map: Variable WhoId Doesn't Exist

If you are looping over a List<Event>, you'll need to pull the WhoId from the loop record: for (Event record : [SELECT WhoId, ... FROM Event WHERE ...]) { if (!events.containsKey(record....
Adrian Larson's user avatar
  • 149k
4 votes
Accepted

Trouble passing variable from Apex to VF Page

The apex code can be modified as below Approach 1 - Using property as getter and setters variable public class ControllerJSGeo{ public String pId; public String testString {get;set;} public ...
Mohith Shrivastava's user avatar
4 votes
Accepted

Pass value from apex component to controller

The component's controller is distinct from the page's controller. To pass the value back to the page, you'd need to pass in the controller through an attribute: Component (Parts) <apex:component&...
sfdcfox's user avatar
  • 482k
4 votes

Lombok for Salesforce Apex

Illuminated Cloud (paid plugin for IntelliJ) has live templates for Apex and Lightning Web Components to help save time. Likewise, you can create your own live templates for IntelliJ if desired. There'...
Kris Goncalves's user avatar
3 votes

What is this technique called?

Well its combination of few, As Robs mentioned its caled lazy loading. Also Its called as Singleton Pattern. Singleton pattern makes sure, you have a single instance of a particular instancein the ...
Pranay Jaiswal's user avatar
3 votes
Accepted

VF Component modifying an apex:variable outside of its scope

apex:variable variables always have a global scope. You necessarily need use different variable names in all components and/or pages (e.g. via apex:include). Rarely, it happens that this behavior is ...
sfdcfox's user avatar
  • 482k
3 votes
Accepted

Flow and InvocableMethod Simple Question

Probably, this could be easy solution. I would suggest to gather a String value in a comma delimited way from the flow and then pass it to the Invocable method. The resultant String will look like ...
Santanu Boral's user avatar
3 votes

Accessing passed variable from Visualforce page OR pass Controller.addFields in TEST

I have made small changes in the Extension class. Added TheOpp = (Opportunity)sc.getRecord(); in the Constructor. public class myextension { private ApexPages.StandardController sc; public ...
Santanu Boral's user avatar
3 votes
Accepted

What is the difference between a variable and an automatic read-write property?

The main reason why there is a distinction is because of Visualforce. You cannot access a variable in Visualforce, but you can access a property. To illustrate this at the most basic level, consider ...
sfdcfox's user avatar
  • 482k
3 votes
Accepted

Soql on Account returns empty Id

In your test class, you're setting PageReference to a variable, this does not adds the parameter to Page's URL in testing mode. A small step you're missing is to set the PageReference with current ...
Raul's user avatar
  • 18.7k
3 votes

Apex variable shows as null

Future methods are called asynchronously. This means that getTokenFromA() will not start until putA() has finished, and in fact, won't even be in the same logical transaction as the original method ...
sfdcfox's user avatar
  • 482k
3 votes

How to switch on RecordTypeId inside a loop?

Per my understanding, you want to group account ids by their RecordTypeId. Consider using a Map for this purpose. It reduces sets where you are storing grouped elements: Map<Id, Set<Id>> ...
Oleksandr Berehovskyi's user avatar
2 votes
Accepted

Define apex:variable in apex:inputText

you can try something like <apex:variable var="input" value="" /> <apex:inputText value="{!input}" styleclass="textCls"/> <apex:commandButton value="Search" onclick="...
Tushar Sharma's user avatar
2 votes

Round off an apex variable upto two decimal places

Use setScale() method to round a decimal Decimal no = 1.23345; Decimal result = no.setScale(2); system.debug(result);
Rohit Mourya's user avatar
  • 3,851
2 votes

Variable does not exist on trigger class

The mq variable is scoped to the loop over your Monthly_Quota__c query. It's not defined once that loop completes, which it does just above the lines where you see this error. Did you mean to use i ...
David Reed's user avatar
  • 92.1k
2 votes

Trouble passing variable from Apex to VF Page

You can set the string in the Constructor as so. public class ControllerJSGeo { public String pId; public String testString {get;set;} public ControllerJSGeo(ApexPages....
Andrew L.'s user avatar
  • 837
2 votes
Accepted

How to pass query variable from soql into loop

Couple of issues here on your current code: RecordType personAccountRecordType = [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'][0].RecordType; Account acc = ...
Jayant Das's user avatar
  • 30.3k
2 votes

How to make variable show the count of records from a dynamic soql query

Since this property is derived, you don't need a setter, and you don't need to perform an assignment in your getter at all. Just do public Integer count { get { return Accounts.size(); ...
David Reed's user avatar
  • 92.1k
2 votes
Accepted

Variable does not exist in test class

Here's your problem: List<ProcessHandlerShowAccounts.AccountParameter> testAcctList = new List <ProcessHandlerShowAccounts.AccountParameter>(); //accountId & name variables do not ...
Adrian Larson's user avatar
  • 149k
2 votes
Accepted

Variable does not exist within invocable method

You have the typo in your parameter at line 5. You should replace accountParamters with accountParameters public class ProcessHandlerShowAccounts { public class AccountParameter{ @...
Himanshu's user avatar
  • 10.5k
2 votes

Variables in Interface in Salesforce

Can you declare variables in Interface in Apex (Salesforce). No, if you want to declare variables, you must use an abstract or virtual class. Also in Apex, are all abstract methods are public by ...
Adrian Larson's user avatar
  • 149k

Only top scored, non community-wiki answers of a minimum length are eligible