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?

2
  • 2
    Create a class with this property, serialize it by saving it e.g. to disk. Commented Jan 10, 2014 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. Commented Jan 10, 2014 at 10:47

3 Answers 3

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....

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

-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.

2 Comments

@down voter Didn't get you??
I was not the down-voter, but did you get the question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.