@Transient indicates that the life cycle of the attribute field exists only in the caller's memory and is not written to disk persisted
If a property is not a field mapping for a database table, it must be marked as @transient, otherwise the ORM framework defaults to its annotation as @basic
Example:
Calculates the age property based on birth
@Transient publicint getage () { return getYear (new CurrentYear ())- getYear (birth); }
As long as an object implements the Serilizable interface, the object can be serialized, but in the actual development process some attributes need to be serialized, and other attributes do not need to be serialized, then the corresponding variable can be added to the transient keyword.
Example:
ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream;Importjava.io.Serializable; Public classTransienttest { Public Static voidMain (string[] args) {User User=NewUser (); User.setusername ("Alexia"); USER.SETPASSWD ("123456"); System.out.println ("Read before Serializable:"); System.out.println ("Username:" +user.getusername ()); System.err.println ("Password:" +user.getpasswd ()); Try{objectoutputstream OS=NewObjectOutputStream (NewFileOutputStream ("C:/user.txt")); Os.writeobject (user); //write the user object into a fileOs.flush (); Os.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } Try{ObjectInputStream is=NewObjectInputStream (NewFileInputStream ("C:/user.txt")); User= (User) is.readobject ();//reading user data from a streamIs.close (); System.out.println ("\nread after Serializable:"); System.out.println ("Username:" +user.getusername ()); System.err.println ("Password:" +user.getpasswd ()); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } Catch(ClassNotFoundException e) {e.printstacktrace (); } }}classUserImplementsSerializable {Private Static Final LongSerialversionuid = 8294180014912103005L; PrivateString username; Private transientString passwd; PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString getpasswd () {returnpasswd; } Public voidsetpasswd (String passwd) { This. passwd =passwd; }}
The output is:
123456null
1) Once the variable is transient modified, the variable will no longer be part of the object persistence, and the variable content cannot be accessed after serialization.
2) The Transient keyword can only modify variables, not methods and classes. Note that local variables cannot be modified by the transient keyword. If the variable is a user-defined class variable, the class needs to implement the serializable interface.
3) Variables modified by the Transient keyword can no longer be serialized, and a static variable cannot be serialized, whether or not it is transient modified.
3rd may be some people are confused, because found in the user class in the Username field with the static keyword, the program operation results are still unchanged, that is, the static type of username also read out as "Alexia", this does not contradict with the 3rd said? This is actually true: 3rd is True (a static variable cannot be serialized regardless of whether it is transient), and the value of the static variable username in the deserialized class is the value of the corresponding static variable in the current JVM. This value is not deserialized in the JVM
Reference from: http://www.cnblogs.com/lanxuezaipiao/p/3369962.html
Use of the Java transient keyword