Any real time example for transient variable in java
Asked Answered
B

6

6

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?

Blasphemous answered 22/1, 2014 at 6:19 Comment(2)
I suppose the type of ts is necessary, since the answer would depend on what you're not serializing...to an extent.Grassi
Check Java collection API like ArrayList. Its helpful if you want to override writeObject/readObject methods.Mugger
G
5

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.

Grassi answered 22/1, 2014 at 6:39 Comment(1)
In that case why to make it a part of entity.Subtlety
S
2

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.

Select answered 22/1, 2014 at 6:28 Comment(0)
M
2

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

Minuscule answered 22/1, 2014 at 6:33 Comment(0)
M
2

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;

Mugger answered 22/1, 2014 at 7:12 Comment(0)
C
1

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.

Calendre answered 22/1, 2014 at 6:26 Comment(2)
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?Blasphemous
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).Calendre
P
1

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.

Perspicacity answered 22/1, 2014 at 6:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.