First, the keyword final
- Final Modified class: This class cannot be inherited
- Final modification method: cannot be overridden
- Final Decorated property: This property is a constant that cannot be assigned once initialized. In practice, constants are represented by uppercase characters.
- Where this constant is assigned: ① This constant cannot use the default initialization ② can be explicitly assigned, code block, constructor
Note: variables are modified with static final: Global Constants
Second, the key word static
Static, statically, can be used for properties, methods, * blocks of code (or initialization blocks), * inner classes
Static modifier property (class variable):
- All objects created by the class share this property
- When one of the objects modifies this property, it causes another object to make a call to this property. VS instance variable (non-static decorated property with each object owning a set of replicas)
- Static class variables are loaded with the load of the class, and the uniqueness
- Static variables can be called directly from the "class. Class variable" form.
- Static class variables are loaded earlier than the object. So when you have an object, you can use the object. Class variable. However, "class. Instance variable" is not possible.
- Static class variables exist in the static domain
Static Modification method (class method):
- Loaded with the load of the class, it is also unique in memory
- Can be called directly from the "Class. Class method" mode.
- You can call a static property or a static method internally, and you cannot invoke a non-static property or method. Conversely, a non-static method can call a static property or a static method
Note: Static structures (properties of static, methods, blocks of code, inner classes) have a lifetime earlier than non-static structures, and are recycled later than non-static structures
The static method inside is not allowed to have this or super keyword!
Static Test instance:
PackageCom.yyx.pratice; Public classJavapratice { Public Static voidMain (string[] args) {account A1=NewAccount ("abc123", 1000); Account A2=NewAccount ("abc456", 2000); Account A3=NewAccount ("abc456", 2000); SYSTEM.OUT.PRINTLN (A1); SYSTEM.OUT.PRINTLN (A2); SYSTEM.OUT.PRINTLN (A3); }}classAccount {Private intId//Account Number PrivateString password;//Password Private DoubleBalance//Balance Private Static Doublerate = 0.05;//Interest Rate Private Static DoubleMinBalance = 1;//Minimum Balance Private Static intinit = 1000; PublicAccount (String password,Doublebalance) { This. id = init++; This. Password =password; This. Balance =balance; } Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; } Public DoubleGetBalance () {returnbalance; } Public voidSetbalance (Doublebalance) { This. Balance =balance; } Public Static Doublegetrate () {returnRate ; } Public Static voidSetrate (DoubleRate ) {Account.rate=Rate ; } Public Static Doublegetminbalance () {returnminbalance; } Public Static voidSetminbalance (Doubleminbalance) {account.minbalance=minbalance; } @Override PublicString toString () {return"Account [id=" + ID + ", password=" + password + ", balance=" + Balance + "]"; }}
The role of the Java keyword final and static