3

Java course assignment:

when a variable is put transient will serialisation ignore the variable or only the value of the variable.

Test this.

How do I test this?

Community
  • 1
  • 1
boselfje
  • 57
  • 7
  • 2
    Create a class with this property, serialize it by saving it e.g. to disk. – Dominik Sandjaja Jan 10 '14 at 10:41
  • 1
    This question makes no sense. Serialization relates to values of variables within an object. The value is serializable, the variable only exists at runtime. Marking a variable as `transient` stops its value being serialized. When deserialized, the variable will still be there, just set to its default value. – Nick Holt Jan 10 '14 at 10:47

3 Answers3

4

Do something like:

public class Test1 implements Serializable {
    private long longValue;
}

public class Test2 implements Serializable {
    private long longValue;
    private transient int intvalue;
}

now serialize an instance of each to disk, if the sizes are the same, then you know that transient variable is not serialized at all, otherwise....

Amir Pashazadeh
  • 7,170
  • 3
  • 39
  • 69
1

If a variable is declared transient it will not be serialized(not stored in bytes stream as state of the Object).

On Deserialization it will get the default value.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
-1

If you don't want to serialize a variable declare it as transient. Serialization means saving the state of a variable. see here for detailed example.

Saurabh Sharma
  • 489
  • 5
  • 20