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 ...
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<...
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
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 ==),...
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 ...
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....
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
...
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 ...
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;
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....
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 ...
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&...
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'...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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>> ...
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="...
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);
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 ...
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....
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 = ...
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();
...
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 ...
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{
@...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
apex-variable × 48apex × 33
visualforce × 12
invocable-method × 5
soql × 3
visual-workflow × 3
trigger × 2
javascript × 2
unit-test × 2
batch × 2
visualforce-component × 2
class × 2
loop × 2
properties × 2
rest-api × 1
email × 1
query × 1
process-builder × 1
integration × 1
controller × 1
code-coverage × 1
email-template × 1
callout × 1
webservices × 1
date × 1