First, let’s talk about final.
The final keyword can modify variables, methods, and classes .
Final variable:
need:
1 Need a compile-time constant that never changes
2 A value initialized at runtime, not wanting to be changed
Benefits: Computes executed at compile time, reducing the burden on runtime
Extensions:
You can modify basic types and reference objects. When modifying the basic type, it means that the value is quite constant. When modifying an object reference, once the reference is initialized and pointed to an object, it cannot be changed to another object (the object itself can be modified)
Blank final
The domain that is modified by final but does not give the initial value must be assigned a value to the final using an expression in the domain definition or constructor (final must be initialized before use)
Notice:
If an object is modified by static and final at the same time (compilation period constant), it is generally expressed in capital, and the underscore link word modification parameters:
If the final modify parameter, it means 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); @Test public void finalDataTest(){ System.out.println(n1); System.out.println("-----------------------------------"); System.out.println(rand.nextInt(30)); System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- * @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, indicating that the method cannot be inherited by subclasses.
Benefits of using final:
1 Before JDK1.5, the efficiency was higher, and after JDK1.5, it can be ignored
2 Method locking to ensure that the meaning of the method in the subclass remains unchanged and cannot be overwritten. Example of usage:
public final String finalMethod(){ return "Hello World" ; } Final class:
If you do not want to be inherited by any class, you can use final to modify the usage example of class:
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 keyword:
Transient can only modify variables, indicating that the variable cannot be serialized.
Generally, we inherit the Serializable interface class, and serialization will be performed automatically. When the variable modified by transient is serialized, it will not be serialized to the specified destination.
so,
1 The variable modified by transient is no longer part of the object persistence, and the content of the variable is serialized and cannot be accessed.
2 Transient can only modify variables, not methods and classes
3 Example of usage of a static variable that cannot be serialized regardless of whether it is modified by transient:
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()); //Serialization 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("序列化后:"); 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
The class that implements the serializable interface is implemented, so serialization will automatically implement the class that implements the Externaliazble interface. Nothing can be automatically serialized, and whether or not using transient has no effect on the result.
If serialization is required, you need to manually specify the variable to be serialized in the writeExternal method.
Example of usage:
public class ExternalizableEx implements Externalizable { private transient String name="ssss"; @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { name=(String) in.readObject(); } @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("Externalizable serialization:"); System.out.println(ex.getName()); //Serialization 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("Externalizable serialization:"); System.out.println(ex.getName()); } catch (Exception e) { e.printStackTrace(); } }} statement:
Most of the final comes from the fourth edition of "Java Programming Thoughts"
Reference article: //www.VeVB.COM/article/86996.htm
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.