+ Start a Discussion
shankar anandshankar anand 

Final keyword usage

I'm learning Apex programming. WHile going through the guides I found this code. I'm confused that 'account' being a Final variable, how come it was successfully used in the update statement in the save() method?

public class MyCustomController {

    private final Account account;

    public MyCustomController() {
        account = [SELECT Id, Name, Site FROM Account 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public Account getAccount() {
        return account;
    }

    public PageReference save() {
        update account;
        return (new ApexPages.StandardController(account)).view();
    }
}
Prabhat Kumar12Prabhat Kumar12
Hi Shankar,

Using final keyword does not mean that we can not use for upadate.

Once you define varaible with Final Keyword it can not be overidden. 

Look in the following documentation for more information.

Using the final KeywordYou can use the final keyword to modify variables.Final variables can only be assigned a value once, either when you declare a variable or in initialization code. You must assign a value to it in one of these two places.
Static final variables can be changed in static initialization code or where defined.
Member final variables can be changed in initialization code blocks, constructors, or with other variable declarations.
To define a constant, mark a variable as both static and final.
Non-final static variables are used to communicate state at the class level (such as state between triggers). However, they are not shared across requests.
Methods and classes are final by default. You cannot use the final keyword in the declaration of a class or method. This means they cannot be overridden. Use the virtual keyword if you need to override a method or class.
shankar anandshankar anand
Hi Prabhat,

As per the documentation provided by you
"Final variables can only be assigned a value once, either when you declare a variable or in initialization code."

So 'account' has been initialised once in the constructor.Now when the user inputs a name in the input field and save the record the new name is updated on the account. So what's the effect of using 'Final' keyword when we can update the variable as many times we want. I didn't ubderstand it at all. Please help.


<apex:page controller="MyCustomController" tabStyle="Account">
<apex:form >
<apex:pageBlock title="Congratulations {!$User.FirstName}"> You belong to Account Name: <apex:inputField value="{!account.name}"/> <apex:commandButton action="{!save}" value="save"/>
</apex:pageBlock>
</apex:form>
</apex:page>

 
Prabhat Kumar12Prabhat Kumar12
Hi Shankar,

The data type of final variable is object. If we use final keyword it means we can't change object of the variable but the value can be changed. 

From Wiki.

If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object. This applies also to arrays, because arrays are objects; if a final variable holds a reference to an array, then the components of the array may be changed by operations on the array, but the variable will always refer to the same array.

Hope this will help you.

Please mark this as answer if it works for you.


Thanks,
Prabhat
shankar anandshankar anand
Hi Prabhat,

Thanks for clarifying. SO, this is not same as in Java where a Final variable can't be modified ryt?
One more thing. I don't understand in what scenario one might try to change the reference object for a Final variable in Apex. Even if I remove Final keyword (say Private Account account), I can't instantiate 'account' variable to say 'new Contact()' or assign the value of Select query on COntact object to the 'account' variable. In both cases I'll get illegal assignment compilation error. Also I can't use the 'account' variable name again to declare a Contact variable as I'll get duplicate variable compilation error. So what is the role of Final here?  I need a scenario where a user might try to change the reference object of a Final variable, I fail to see the point of using Final.
Ankit Gupta SFDCLearnerAnkit Gupta SFDCLearner
Hi Prabhat,

As per salesforce guide, variable can be assigned at most once. It's not required that datatype id object or not. Please check below link.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_constants.htm

Thanks,
Salesforce Learner