- Transient keyword is only applicable on member variable or fields.
- You can mark a variable as transient and static at the same time.
- Transient variables are not saved when an object gets serialized.
Using the Transient Keyword in Salesforce
Transient keyword in Salesforce is a less common keyword than other volatile keywords. Since it is not as common as other keywords, this makes it necessary to know how and where to use it. Transient keyword in Salesforce can be described as the process of serialization that is geared to prevent variables from being serialized. This means in case you are in possession of a field which doesn’t fit serialization, you can label it transient and thus it won’t be serialized.
What is transient variable? In Java, a transient variable is described as a variable which has been modified with a transient keyword, thus becoming a transient variable. Thus, by modifying any variable by use of any transient keyword, you are able to create a transient variable in Java.
In Java, serialization involves the process of saving an object’s state by JMV. Likewise, during deserialization you use the same JMV to recover the object. During this process of serialization, all the properties of an object are saved apart from the transient and static properties. This means that if you are saving an object, but don’t want to save some specific properties, you can mark them as static or transient.
Once you mark them this way, the JVM will not serialize them, hence won’t be saved. However, there is one key point you ought to note before labeling any property as transient for it not to be serialized. You should always ensure to give the property you are saving as transient a default value during the process of deserialization. This is to enable the property function properly, otherwise if you don’t, the deserialized property will not be usable.
Importance of Transient Variable in Java
Transient variables are very important in Java language. Why? This is because they give you some kind of control over the process of serialization. They too allow you some flexibility that enables you to leave out some properties of objects from the serialization process. It is at times sensible not to serialize certain properties of objects, thus transient variable helps you leave such properties out.
Which Are Some Of the Variables You Can Mark As Transient?
Which are some of the variables that one can mark as transient? How do you mark a variable as transient? It is good to know which variable one can mark as transient. One thing to note is that any one variable whose value one can obtain from calculation of other variables does not need to be saved. For instance, if there is a field, say “distance” whose value you can calculate from fields “speed and time”, you don’t need to save distance. There is no need to serialize distance since you only need to multiply other variables, speed and time, to obtain distance.
Notable Points about Transient Keyword
Here are some very vital points concerning transient variables that you ought to know.