The transient of the Java language is not as well known as class, synchronized, and other familiar keywords, so it appears in some interview questions. This article I will explain transient for you.
Transientthe use of
What does the Q:transient keyword accomplish?
A: When an object is serialized (writes a sequence of bytes to the target file), transient blocks those variables declared with this keyword persisted in the instance, and when the object is deserialized (the byte sequence is read from the source file ), such instance variable values are not persisted and restored. For example, when deserializing an object-a data flow (for example, a file) may not exist, because a variable of type Java.io.InputStream exists in your object, the input stream referenced by these variables cannot be opened when serialized.
Transient usage Introduction
Q: How do I use transient?
A: Contains the transient modifier in the instance variable declaration. Fragment 1 provides a small demo.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
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: serialization and deserialization of 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 an Classlib instance.
Classlib declares that its instance variable is transient, because it can serialize an input stream without meaning (as described above). In fact, if this variable is not transient, it throws java.io.NotSerializableException when deserializing the X.ser content, because InputStream does not implement the serializable interface.
Compile fragment 1:javac Transdemo.java; Run the application with a parameter transdemo.class: Java transdemo transdemo.class. You may see output similar to the following:
| 1 2 3 4 5 6 7 8 |
ClassLib(InputStream) called Minor version number: 0 Major version number: 51 [email protected] Minor version number: 0 Major version number: 51 null |
The above output shows that when the object is refactored, no method calls are constructed. In addition, is assumes that the default is null, compared to when Classlib objects are serialized, Majorver and Minorver are valued.
The member variables in the class and thetransient
Q: Can I use transient in a member variable in a class?
A: The answer to the question, see fragment 2.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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 the Foo object is serialized and deserialized, not classlib. In addition, Foo contains a pair of variables, W and X, as well as instance variables Y and Z.
Compile Fragment 2 (Javac Transdemo.java) and run the app (Java Transdemo). You can see the following output:
| 1 2 3 4 5 6 7 8 9 |
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 and Z is not, and it is marked transient. However, when Foo is serialized, it does not tell us whether the variables W and X are serialized and deserialized, and whether they are only initialized in the normal class initialization mode. For the answer, we need to look at the contents of the X.ser.
The following shows the x.ser 16 binary:
| 0 S |
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.... |
Because of the "the Java serialization algorithm revealed" article in Javaworld, we found the meaning of the output:
-
AC ED Serialization Protocol identification
-
00 05 Stream Version Number
-
73 means this is a new object
-
72 means that this is a new class
-
00 03 indicates the class name length (3)
-
6F 6 F denotes class name (Foo)
-
FC 7A 5D 1D D2 9D 3 F indicates the serial version identifier of the class
-
02 indicates that the object supports serialization
-
00 01 indicates the number of variables for this class (1)
-
49 Variable Type code (0x49, or I, which represents int)
-
00 01 indicates variable name length (1)
-
79 variable name (y)
-
78 indicates that the object is an optional data block end
-
70 means we've reached the top of the class hierarchy
-
00 00 00 03 = value of Y (3)
It is obvious that only instance variable y is serialized. Because z is transient, it cannot be serialized. In addition, even if they mark Transien,w and X cannot be serialized, the reason is that their class variables cannot be serialized.
How to use transient in Java