First, say final. The
Final keyword can be decorated with variables, methods, class .
final variable:
requirements:
1 requires a never-changing compile-time amount of
21 run-time initialized values that do not want to be changed
Benefits: performs calculations at compile time, mitigating the burden of the runtime
extension:
can modify the base type and reference object. When you modify a basic type, you indicate that the value is constant. When an object reference is decorated, once the reference is initialized to an object, it cannot be changed to another object (the object itself can be modified)
blank final
Final modified but not given an initial value
Final assignment must be given with an expression in the definition or constructor of a domain (before final use must be initialized)
Note:
If an object is decorated with both static and final (compile-time constants), typically in uppercase, the underlined link word
Cosmetic parameter:
If the final cosmetic parameter 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 (m);
private static final int number2=random.nextint (a);
@Test public
void Finaldatatest () {
System.out.println (n1);
System.out.println ("--------------------");
System.out.println (rand.nextint);
System.out.println ("--------------------");
System.out.println ("After the initial compilation, will not change:" +number);
System.out.println ("--------------------");
System.out.println ("Does not change after the initial compilation:" +number2);
}
/**
* Final modifier parameter: This parameter is readable but cannot be modified.
* @param SK
* @return
/public string Finalparam (final String SK) {
//sk= "Jeyson"; Final parameter cannot be modified return
SK;
}
Final method:
Final can also modify the method to indicate that the method cannot be inherited by the quilt class.
The benefits of final use:
1 JDK1.5 before, the efficiency is higher, JDK1.5 can be neglected later
2 method lock to ensure that the method in the subclass does not change its meaning and cannot be overwritten
Usage examples:
Public final String Finalmethod () {return
' Hello world ';
}
Final class:
You do not want to be inherited by any class, you can use the final modifier class
Usage examples:
Public final class Finalclasstx {
private int k;
public void Getmyword () {
System. Out. println ("This is a final class, the value of K is" +GETK ());
}
public int getk () {return
k;
}
public void setk (int k) {this
. k = k;
}
}
Then transient the keyword:
Transient can only modify variables, indicating that the variable cannot be serialized.
Generally we inherit the class of the Serializable interface, serialization is done automatically, and variables decorated with transient are not serialized to the specified destination when the class is serialized.
So
1 transient-modified variables are no longer part of the object's persistence, and the contents of the variable cannot be serialized for access
2 transient can only modify variables, cannot modify methods and classes
3 A static variable, whether or not modified by transient, cannot be serialized
Usage examples:
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 ());
Serializes the 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 ("After serialization:");
System.out.println ("Username=" +user.getusername ());
System.out.println ("password=" +user.getpassword ());
catch (Exception e) {e.printstacktrace ();
} System.out.println ("--------------------------------");
} class User implements serializable{ Private static final long serialversionuid = 1L;
Private String username;
Use transient private 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
implements the class of the Serializable interface, so serialization automatically makes
the class that implements the Externaliazble interface, and nothing can be automatically serialized. Whether or not you use transient has no effect on the results.
If serialization is needed at this point, you need to manually specify the variable you want to serialize in the Writeexternal method.
Using the example:
public class Externalizableex implements externalizable {private transient String name= "ssss"; @Override public void Readexternal (ObjectInput in) throws IOException, ClassNotFoundException {name= (String) In.readob
Ject ();
@Override public 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 ()); Serializes the try {objectoutputstream os=new objectoutputstream (New FileOutputStream ("C://myresource//test2.txt")
)));
Os.writeobject (ex);
Os.flush ();
Os.close ();
catch (Exception e) {e.printstacktrace (); ///deserialization try {ObjectInputStream is=new objectinputstream (New FileInputStream ("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 Most of it comes from the fourth edition of Java programming ideas
Reference article: http://www.jb51.net/article/86996.htm
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.