Java keywords --- final and transient, --- finaltransient
First, let's talk about final.
Final keywords can be used to modify variables, methods, and classes.
Final variable: Requirement: 1. A constant Compilation Time is required. 2. The value initialized during the runtime. You do not want to change the value. benefit: the computation executed during the compilation, reduce the workload during running: you can modify basic types and reference objects. When modifying the basic type, the value remains unchanged. When modifying an object reference, once the reference is initialized to point to an object, it cannot be changed to another object (the object itself can be modified) blank final modifier but does not give the initial value of the domain must be defined in the domain or in the constructor using an expression to assign a value to final (final must be initialized before use) Note: if an object is modified at the same time by both static and final (a constant during compilation), it is generally written in uppercase. The underline link word modifies the parameter: If final modifies the parameter, it indicates that the parameter is readable but cannot be modified. Usage example:
Private Random rand = new Random (); private static Random random = new Random (); private final int n1 = 12; private final int number = rand. nextInt (30); private static final int NUMBER2 = random. nextInt (40); @ Testpublic void finalDataTest () {System. out. println (n1); System. out. println ("--------------------"); System. out. println (rand. nextInt (30); System. out. println ("--------------------"); System. out. println ("after the initial compilation, no Changed: "+ number); System. out. println ("--------------------"); System. out. println ("it will not be changed after compilation:" + NUMBER2);}/*** final modifier parameter: this parameter is readable but cannot be modified. * @ Param sk * @ return */public String finalParam (final String sk) {// sk = "jeyson"; the final parameter cannot be changed to return sk ;}Final method:Final can also be modified to indicate that the method cannot be inherited by the quilt class. Advantages of Using final: 1. JDK1.5 was previously more efficient. After JDK1.5, you can ignore the locking of method 2 to ensure that the meaning of the method in the subclass remains unchanged and cannot be overwritten. Example:
public final String finalMethod(){ return "Hello World" ; }
Final class:If you do not want to be inherited by any class, you can use the final modifier class usage example:
Public final class FinalClassTx {private int k; public void getMyWord () {System. out. println ("this is a final class, and the value of k is" + getK ();} public int getK () {return k;} public void setK (int k) {this. k = k ;}}
Then the transient keyword is:
Transient can only modify a variable, indicating that the variable cannot be serialized. Generally, we inherit the class of the Serializable interface, and the serialization will automatically proceed. Variables modified with transient will not be serialized to the specified destination when the class is serialized. Therefore, a variable modified by transient is no longer part of Object Persistence. The variable content serialization cannot obtain access 2 transient and can only modify the variable, the method and class 3 static variables cannot be serialized, whether or not they are modified by transient. Example:
Public class TransientEx {public static void main (String [] args) {User user = new User (); user. setUsername ("jeyson"); user. setPassword ("123456"); System. out. println ("before serialization:"); System. out. println ("username =" + user. getUsername (); System. out. println ("password =" + user. getPassword (); // serialize try {ObjectOutputStream OS = new ObjectOutputStream (new FileOutputStream ("C: // MyResource // test1.txt"); OS. writeObject (user); OS. flush (); OS. close ();} catch (Exception e) {e. printStackTrace ();} // deserialization try {ObjectInputStream is = new ObjectInputStream (new FileInputStream ("C: // MyResource // test1.txt"); user = (User) is. readObject (); is. close (); System. out. println ("serialized:"); System. out. println ("username =" + user. getUsername (); System. out. println ("password =" + user. getPassword ();} catch (Exception e) {e. printStackTrace ();} System. out. println ("inline") ;}} class User implements Serializable {private static final long serialVersionUID = 1L; private String username; // use transientprivate transient String password; public String getUsername () {return username;} public void setUsername (String username) {this. username = username;} public String getPassword () {return password;} public void setPassword (String password) {this. password = password ;}}
Extension: Externalizable
Class that implements the serializable interface, so the serialization will automatically
Class that implements the Externaliazble interface. Nothing can be automatically serialized, regardless of whether transient is used or not. If serialization is required, manually specify the variable to be serialized in the writeExternal method. Example:
Public class ExternalizableEx implements Externalizable {private transient String name = "ssss"; @ Overridepublic void readExternal (ObjectInput in) throws IOException, ClassNotFoundException {name = (String) in. readObject () ;}@ Overridepublic void writeExternal (ObjectOutput out) throws IOException {out. writeObject (name);} public String getName () {return name;} public void setName (String name) {this. name = name;} public static void main (String [] args) {ExternalizableEx ex = new ExternalizableEx (); ex. setName ("jeyson"); System. out. println ("before Externalizable serialization:"); System. out. println (ex. getName (); // serialize try {ObjectOutputStream OS = new ObjectOutputStream (new FileOutputStream (new File ("C: // MyResource // test2.txt"); OS. writeObject (ex); OS. flush (); OS. close ();} catch (Exception e) {e. printStackTrace ();} // deserialization try {ObjectInputStream is = new ObjectInputStream (new FileInputStream (new File ("C: // MyResource // test2.txt "))); ex = (ExternalizableEx) is. readObject (); is. close (); System. out. println ("after Externalizable serialization:"); System. out. println (ex. getName ();} catch (Exception e) {e. printStackTrace ();}}}
Statement:
Final is mostly from the fourth edition of java programming ideas.
Transient references from: http://www.cnblogs.com/lanxuezaipiao/p/3369962.html