Any real time example for transient variable in java

19,725

Solution 1

To put it plainly: any field marked as transient is not saved if/when the object is serialized.

Let's say, for instance, you have an entity that needs to be serialized, but this entity has a data field that is, in a word, massive:

public class SerializeMe implements Serializable {
    private Integer id;
    private String value;
    private BigMatrix bigMatrix = BigMatrixFactory.fromValues(id, value).build();

    public BigDecimal doExpensiveOperation() {
        BigDecimal result = BigDecimal.ZERO;
        for(BigDecimal value : bigMatrix.getAllValuesFromAllMatrixFields() {
            matrixMultiply(result, value);
        }
        return result;
    }
}

You really don't want that BigMatrix to be serialized, since you'd be serializing a huge quantity of data which isn't necessary to rebuild the object (note, it can be built simply by using the builder pattern, given the ID and value).

In a trivial example, this may not be that big of a deal. But if you were sending this particular entity across a network, that would be a huge deal - you now have the expense of transferring not only the entity you want, but also some internal state about it that can be generated.

Solution 2

You can make fields transient if they can be calculated in runtime, which is much cheaper to serialize and deserialize them.

Solution 3

A transient variable is a variable that can not be serialized. the transient keyword can be used to indicate the Java virtual machine that the variable is not part of the persistent state of the object.

This can be used in a scenario where only some of the fields in a class are required to be saved and others are actually dervied from the existing .

class MyExample  implements Serializable
{
    private Date currentDate;
    private transient String dateValueInString;     // this will not be saved

    // This methos provides the date in the format 2 feb
    private void generatederivedValue()
    {
       dateValueInString = currentDate.getDay() + " " +   convertToStringMonth(currentDate.getMonth());
    }
}

So when the object of the above class is serialized , the dateValueInString will not be saved. This would help in saving space when thousands of the objects of this class are serialized.

Solution 4

Following cases --

  1. Declaring attribute as transient is particularly helpful if you have some secured information like password, SSN number etc.
  2. APIs like ArrayList; provides its own serialization mechanism. This can be done by declaring memeber as transient as shown below :

    private transient Object[] elementData;

Solution 5

If you intend to serialize your objects, but there are fields that aren't strictly a part of the object (such as ones that are for example computed at runtime from other values), those should be transient so they wouldn't be serialized.

Share:
19,725
Shashi
Author by

Shashi

Learning new curve everyday..

Updated on August 16, 2022

Comments

  • Shashi
    Shashi almost 2 years

    From the question Why does Java have transient fields?. I am able to understand the transient. But, not able to evaluate to using transient keyword at the time of designing.

    public class A implements Serializable 
    {
        public String s;
        public transient ts; 
    }
    

    If i do the same thing in alternative way..

    public class A implements Serializable 
    {
        public String s;
        //public transient ts;//removing this variable.
    
    } 
    

    And use another class and define method inside the class and define the variable ts and do the operation and persist the value of s as business defines.

    QUESTION

    I didn't find any real time example in the web where i will take decision to define a variable transient.

    How could i take the decision to define transient at the time of designing? Is there any realtime scenario that helps me to understand?

  • Shashi
    Shashi over 10 years
    My Question is not about the "what is transient". How to decide where to define 'transient' at the time of design level? You can check the two class above.. Is there anything wrong if i go for 2nd approach?
  • Kayaman
    Kayaman over 10 years
    And I answered you. Fields that aren't a part of the object, such as ones that are computed from other fields or fields that don't need to be persisted (or rather, retrieved).
  • Tushar Banne
    Tushar Banne over 7 years
    In that case why to make it a part of entity.