Hello everyone, in this video we are going to see the difference between static variable
declaration and static final variable declaration. Static keyword is used to make the variable value the same for every instance of the class
If I declare static string strStaticVariable equal to testing, whenever I wanted to make
use of this variable value, I have to use class name dot the variable name
The value will not change unless we change it. If I change it, the value will be changed only during that particular transaction
Once I come out of the transaction, if another user creates another transaction, the value
will be still testing. One important thing is we have to make sure we are using class name dot variable name
notation so that we can get the value or we can set the value
The final keyword along with static will make the variable like a constant or it will make
the value unchangeable. Whenever we use final static, sorry static final along with static if I use final and
if I assign a value, then I won't be able to change the value at all
It is like a constant. If I wanted to access the value, I can make use of class name dot variable name
So let's see this sample class. So I have public class sample class and I have two variables
One is static and the other one is static final. The strStaticVariable is testing and strFinalStaticVariable is equal to example
Now both are public access identifier. So I will be able to access it from class name
I have a system dot debug statement. This is Salesforce Apex coding system to debug statement where I have before change strStaticVariable
is class name dot variable name strStaticVariable. And then at line number 20, I'm changing the value to value changed
It won't throw an error because this static variable is static variable
It is not static final. And I have another debug statement. After change strStaticVariable is class name dot variable name
So it should display value changed. First time it should display testing
And I have another code at line number 32 where I have a sample class dot strFinalStaticVariable
equal to test. Here it will throw an error. The exception will be system dot final exception
Final variable has already been initialized. So I won't be able to change the value
So it will throw this exception. Since this value is already defined in the class, we cannot define it in the calling
class or when we do some coding and then we wanted to change it at the runtime
So at the runtime, we won't be able to change it. If I wanted to display it, we can try
We can do that
So at line number 25 to 28, I am just displaying what is the value of strStaticVariable value
Let's run this
So first it should display testing because the value of strStaticVariable is testing
So it should display testing. Next I'm changing the value of strStaticVariable
So it should display value changed. Next it should display example and it should throw me an exception stating that I won't
be able to initialize, sorry, I won't be able to set the value for static final variable
if it has been already been initialized. So let's see that. So I got the exception
Let's look at the debug statement. So before change, strStaticVariable is testing and after that it is value changed
I was able to access the static final variable and its value is example
And then I tried to change it at line number 21 and then it failed
It failed because I won't be able to set a value if it has been already been initialized
This keyword static and final makes it like a constant or unchangeable
I hope it was helpful
Thank you for watching. I'm going to go ahead and close this. I'm going to close this
I'm going to close this. I'm going to close this. I'm going to close this. I'm going to close this
I'm going to close this. I'm going to close this