Serialization/Deserialization of the final transient fields
Asked Answered
L

2

3

In this question says that final transient fields can not be set to any non-default value after serialization. So why I have 3 for aVar1 variable and s3 for aVar3 variable?

import java.io.*;
import java.util.*;

class Test
{
    public static void main(String[] args) throws IOException, ClassNotFoundException
    {
        A a1 = new A();
    
        // save a1 to file
        FileOutputStream fileOutput = new FileOutputStream("a.dat");
        ObjectOutputStream outputStream = new ObjectOutputStream(fileOutput);
        outputStream.writeObject(a1);
        fileOutput.close();
        outputStream.close();
    
        // load a1 from file
        FileInputStream fiStream = new FileInputStream("a.dat");
        ObjectInputStream objectStream = new ObjectInputStream(fiStream);
        a1 = (A) objectStream.readObject();
        fiStream.close();
        objectStream.close();
    
        // fields after deserialization
        System.out.println(a1.aVar1); // 3
        System.out.println(a1.aVar2); // null
        System.out.println(a1.aVar3); // s3
        System.out.println(a1.aVar4); // null
    }
}

class A implements Serializable
{
    public final transient int aVar1 = 3;
    public final transient Map <Object, Object>  aVar2 = new HashMap <> ();
    public final transient String aVar3 = "s3";
    public final transient String aVar4 = new String("s4");
}
Leatriceleave answered 18/1, 2021 at 3:22 Comment(0)
R
1

During deserialization, the constructor of the object is not called. This is a special object instantiation process handled by the JVM.

For aVar2 and aVar4, the Hashmap and string constructor is called. So these variables are assigned default values(null).

For aVar1 and aVar3, some constant expression is assigned to them. These are called compile time constants.

The conditions for compile time constants are

  1. They must be declared final
  2. They are of primitive data types or String
  3. They must be initialized with their declaration.
  4. Their value must be constant expression.

Compile time constants are reaffected and these values will be retained after deserialisation.

Rogovy answered 18/1, 2021 at 4:55 Comment(0)
V
0

aVar1 and aVar3 are compile time constants, these are never read via instance fields, but always directly.

So it looks like you actually do a1.aVar1, i.e. you access an instance field - but you never do. It's like doing: System.out.println(3), directly.

On the other hand, the rest of the instance fields are not compile time constants, according to the JLS.

Valentijn answered 18/1, 2021 at 4:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.