Question

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?

Était-ce utile?

La solution

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.

Autres conseils

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.

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

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;

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.

As a guideline, you should serialize variables that you wish to be able to restore that you would otherwise be incapable of restoring programmatically. Similarly, you should ensure that you're not saving data unnecessarily; if a variable can be easily repopulated based on processing of other serializable data, you should take that route.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top