search
HomeJavajavaTutorialHow to use transient in Java

How to use transient in Java

Feb 20, 2017 am 10:13 AM
javatransient

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
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software