Home  >  Article  >  Java  >  How to use transient in Java

How to use transient in Java

黄舟
黄舟Original
2017-02-20 10:13:291489browse

Transient in the Java language is not as well-known as class, synchronized and other familiar keywords, so it will appear in some interview questions. In this article I will explain transient to you.

Use of transient

Q: What can the transient keyword achieve?

A: When the object is serialized (writes the byte sequence to the target file), transient prevents the variables declared with this keyword in the instance from being persisted; when the object is deserialized (from The source file reads the byte sequence for reconstruction), such instance variable values ​​will not be persisted and restored. For example, when deserializing an object - a data stream (for example, a file) may not exist, the reason is that there are variables of type java.io.InputStream in your object, and the input streams referenced by these variables cannot be opened during serialization. .

Introduction to using transient

Q: How to use transient?

A: Contains the transient modifier in the instance variable declaration. Snippet 1 provides a small demonstration.

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io .IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class ClassLib implements Serializable {
    private transient InputStream is;
    private int majorVer;
    private int minorVer;
    ClassLib(InputStream is) throws IOException {
        System.out.println("ClassLib(InputStream) called");
        this.is = is;
        DataInputStream dis;
        if (is instanceof DataInputStream)
            dis = (DataInputStream) is;
        else
            dis = new DataInputStream(is);
        if (dis.readInt() != 0xcafebabe)
            throw new IOException("not a .class file");
        minorVer = dis.readShort();
        majorVer = dis.readShort();
    }
    int getMajorVer() {
        return majorVer;
    }
    int getMinorVer() {
        return minorVer;
    }
    void showIS() {
        System.out.println(is);
    }
}
public class TransDemo {
    public static void main(String[] args) throws IOException {
        if (args.length != 1) {
            System.err.println("usage: java TransDemo classfile");
            return;
        }
        ClassLib cl = new ClassLib(new FileInputStream(args[0]));
        System.out.printf("Minor version number: %d%n", cl.getMinorVer());
        System.out.printf("Major version number: %d%n", cl.getMajorVer());
        cl.showIS();
        try (FileOutputStream fos = new FileOutputStream("x.ser");
                ObjectOutputStream oos = new ObjectOutputStream(fos)) {
            oos.writeObject(cl);
        }
        cl = null;
        try (FileInputStream fis = new FileInputStream("x.ser");
                ObjectInputStream ois = new ObjectInputStream(fis)) {
            System.out.println();
            cl = (ClassLib) ois.readObject();
            System.out.printf("Minor version number: %d%n", cl.getMinorVer());
            System.out.printf("Major version number: %d%n", cl.getMajorVer());
            cl.showIS();
        } catch (ClassNotFoundException cnfe) {
            System.err.println(cnfe.getMessage());
        }
    }
}

Fragment 1: Serializing and Deserializing ClassLib Objects

The ClassLib and TransDemo classes are declared in Fragment 1. ClassLib is a library that reads Java class files and implements the java.io.Serializable interface so that these instances can be serialized and deserialized. TransDemo is an application class used to serialize and deserialize ClassLib instances.

ClassLib declares its instance variables as transient because it can meaninglessly serialize an input stream (as described above). In fact, if this variable is not transient, java.io.NotSerializableException will be thrown when deserializing the contents of x.ser because the InputStream does not implement the Serializable interface.

Compile fragment 1: javac TransDemo.java; run the application with one parameter TransDemo.class: java TransDemo TransDemo.class. You may see output similar to the following:

ClassLib(InputStream) called
Minor version number: 0
Major version number: 51
java.io.FileInputStream@79f1e0e0
Minor version number: 0
Major version number: 51
null

The above output shows that when the object is reconstructed, no constructor method is called. Additionally, is is assumed to default to null, in contrast to majorVer and minorVer which have values ​​when the ClassLib object is serialized.

Member variables and transient in a class

Q: Can transient be used in member variables in a class?

A: For the answer to the question, please see Fragment 2

public class TransDemo {
    public static void main(String[] args) throws IOException {
        Foo foo = new Foo();
        System.out.printf("w: %d%n", Foo.w);
        System.out.printf("x: %d%n", Foo.x);
        System.out.printf("y: %d%n", foo.y);
        System.out.printf("z: %d%n", foo.z);
        try (FileOutputStream fos = new FileOutputStream("x.ser");
                ObjectOutputStream oos = new ObjectOutputStream(fos)) {
            oos.writeObject(foo);
        }
        foo = null;
        try (FileInputStream fis = new FileInputStream("x.ser");
                ObjectInputStream ois = new ObjectInputStream(fis)) {
            System.out.println();
            foo = (Foo) ois.readObject();
            System.out.printf("w: %d%n", Foo.w);
            System.out.printf("x: %d%n", Foo.x);
            System.out.printf("y: %d%n", foo.y);
            System.out.printf("z: %d%n", foo.z);
        } catch (ClassNotFoundException cnfe) {
            System.err.println(cnfe.getMessage());
        }
    }
}

Fragment 2: Serializing and Deserializing Foo Objects

Fragment 2 is somewhat similar to Fragment 1. But the difference is that it is the Foo object that is serialized and deserialized, not ClassLib. Furthermore, Foo contains a pair of variables, w and x, and instance variables y and z.

Compile fragment 2 (javac TransDemo.java) and run the application (java TransDemo). You can see the following output:

w: 1
x: 2
y: 3
z: 4
w: 1
x: 2
y: 3
z: 0

This output tells us that the instance variable y is serialized, but z is not. It is marked transient. However, when Foo is serialized, it does not tell us whether the variables w and x are serialized and deserialized, or whether they are just initialized in the normal class initialization manner. For the answer, we need to look at the contents of x.ser.

The x.ser hex is shown below:

00000000 AC ED 00 05 73 72 00 03 46 6F 6F FC 7A 5D 82 1D ....sr..Foo.z]..
00000010 D2 9D 3F 02 00 01 49 00 01 79 78 70 00 00 00 03 ..?...I..yxp....

Thanks to the article "The Java serialization algorithm revealed" in JavaWorld, we found out the meaning of the output:

AC ED serialization protocol identification

00 05 stream version number

73 means this is a new object

72 means this is a new class

00 03 Represents the class name length (3)

46 6F 6F Represents the class name (Foo)

FC 7A 5D 82 1D D2 9D 3F Represents the serial version of the class Identifier

02 indicates that the object supports serialization

00 01 indicates the number of variables of this class (1)

49 Variable type code (0×49, or I, Represents int)

00 01 Represents the variable name length (1)

79 Variable name (y)

78 Represents the end of the optional data block of the object

70 indicates that we have reached the top of the class hierarchy

00 00 00 03 indicates the value of y (3)

Obviously, only the instance variable y is serialized. Because z is transient, it cannot be serialized. Furthermore, even if they are marked transient, w and x cannot be serialized, because their class variables cannot be serialized.

The above is the content of how to use transient in Java. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn