The summary of Finalkeyword is very easy to learn and master, roughly divided into final modified symbolic constants, classes, methods, attributes four parts. And these four parts of the content is what we have to master, very useful. In the project, we often use Finalkeyword to help complete our specific tasks. Here's a look at finally final:
Symbolic Constants
The final modified variable is called the last variable. Also known as symbolic constants.
Like what:
Double PI = 3.14;
===>>> Final Double PI = 3.14; Symbolic constants
Demo
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >package Com.hqg.oop.d37.a3;public class Finaltest {public static void main (string[] args) {final double PI = 3.14;//pi = 3.1415926; A symbolic constant cannot change its value halfway. SonDemo1 sd = new SonDemo1 (); int re = Sd.add (10, 20); System.out.println (re);}} Class Person{private string name;private final boolean sex ;//finally property (read only property) public person (String name, Boolean sex) {T His.name = Name;this.sex = Sex;}} Class Demoa {public final int add (int x, int y) {return x + y;}} Class SonDemo1 extends Demoa {}//class Son extends String {////}final class Father {}//class Son extends Father {////} </span></span>
class
A class that is finally decorated, that is, the class.
Characteristics:
It cannot have subclasses. That is, anti-inheritance.
For example, the String class provided by an expert is the last class.
Method
The final modified method. That is the last method.
Characteristics:
It cannot be rewritten. That is: anti-rewrite.
Properties
An instance variable that is finally modified is a final attribute and also a read-only property.
Characteristics:
1) It must be modified by the final.
2) Once it is assigned a value. will be the same for life. (not change halfway)
3) When declaring the last instance variable. If there is no assignment, it must be assigned a value in each constructor.
4) Finally, the instance variable does not have a set.
5) Finally, the instance variable name is capitalized.
Demo
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >package Com.hqg.oop.d38.a1;public class Finalobjectvartest {public static void main (string[] args) {person P1 = new Pe Rson ("Talisman", false, 18); person P2 = new Person ("Talisman", false, 18); System.out.println (p1); System.out.println (p2); System.out.println (P1.equals (p2));//p1. SEX = True;}} Class Person {//instance variable private String name;public final Boolean SEX;//Contract: True for male, false for female private int age;public person () {I NT n = (int) (2 * math.random ()); SEX = n = = 1;} Public person (String Name,int age) {This (), this.name = Name;this.age = age;} Public person (String name, boolean sex, int age) {this.name = name; SEX = Sex;this.age = age;} public void SetName (String name) {this.name = name;} public void Setage (int.) {this.age = age;} Reader public String GetName () {return name;} public Boolean issex () {return SEX;} public int getage () {return age;} @Overridepublic String toString () {return name: "+ name +" Gender: "+ (sex?") Male ":" female ") +" age: "+ age;} @Overridepublic boolean equals (Object obj) {person pp = (person) obj;if (this.name.equals (pp.name) && this. SEX = = pp. SEX && this.age = = pp.age) {return true;} Else{return false;}}} </span></span>
Business Ideas
Like a proverb in English: "Each coin have two sides" (each coin has both sides), keywordfinal just like that coin. Using final to define symbolic constants, classes, methods, and properties can reduce our ability to work very much and increase the fault tolerance rate.
[JAVA & #183; 0 basic]:11. Terminator-final