当前位置:网站首页>Explain final, finally, and finalize
Explain final, finally, and finalize
2022-06-28 09:28:00 【The green flowers of Wang Li's family】
One 、final keyword
1.1 decorator
When it modifies a class, it means that the class cannot be inherited , Because abstract classes are used to be inherited , therefore abstract Key words and final Keywords cannot coexist .final All member methods in a class are implicitly defined as final Method .
1.2 Modification methods
When a method is declared as final when , This method does not allow any subclass to override this method , But subclasses can still use this method .
PS: The difference between rewriting and overloading : Method overloading and rewriting are ways to implement polymorphism , The difference is that the former implements compile time polymorphism , The latter implements runtime polymorphism .
heavy load :
Overload occurs in a class , If a method with the same name has a different parameter list ( Different parameter types 、 The number of parameters is different or both are different ) be
Considered overloaded , Overload has no special requirements for return type , Cannot differentiate by return type .
rewrite :
1. Occurs between parent and child classes
2. Method name , parameter list , Return type ( Except that the return type of the method in the subclass is the subclass of the return type in the parent class ) It has to be the same
3. The access modifier limit must be greater than the access modifier of the overridden method (public>protected>default>private)
4. The overriding method must not throw a new check exception or a more general check exception than the overridden method declaration
1.3 Modifier constant
When final When modifying a basic data type , Indicates that the value of the basic data type cannot change once it is initialized ; If final When modifying a reference type , After it is initialized, it can no longer point to other objects , But the content of the object that the reference refers to can change . It's one thing in essence , Because the value of the reference is an address ,final Required value , That is, the value of the address does not change .—— Reference immutable
final Member variables represent constants , Can only be assigned once , The value does not change after assignment .final Modify a member variable ( attribute ), Initialization must be displayed . There are two initialization methods , One is to initialize when a variable is declared ; The second method is to declare a variable without assigning an initial value , However, the initial value should be assigned to the variable in all constructors of the class in which the variable is located .
As shown below :
When a This attribute is decorated as final, Instead of static When , It belongs to the resource of the instance object of the class . When static final When decorating at the same time , It belongs to class resources .
When a class is loaded into memory , Because it is the resource of the instance object , This property is not allocated memory space , It just defines a variable a, Only when the class is instantiated ( Create objects ) This attribute is used to allocate memory space , When instantiating, the constructor is executed at the same time , So the property is initialized , This is consistent with the need to initialize when it is allocated memory space .
Two 、finally keyword
finally As part of exception handling , It can only be found in try/catch In the sentence , And a statement block is attached to indicate that the statement must be executed in the end , Often used in situations where resources need to be released .
finally Not being carried out :
(1) Only with finally Corresponding try When the statement block gets executed ,finally Statement block will execute .
(2)try Terminated in statement block Java The running or thread of the virtual machine is interrupted interrupted Or terminate .
public static int a() {
int a = 0;
try{
a++;
return a;
}catch (Exception e){
a++;
return a;
}finally {
a++;
return a;
}
}
The output of the above code is :2.
finally Statement must be executed in the modified code , Throw or not throw the exception , Will have a first time a++, And then finally Once in a++.
Sum up , In both return Under the circumstances ,finally Medium return For the end result .
3、 ... and 、finalize() Method
Refer to another article of mine :JVM series (2)—— Garbage collection
finalize() The method is Object class , Because all classes inherit from Object class , So all classes have finalize Method .
Before an object is actually declared dead , It will be marked twice : The first is after reachability analysis , Found recyclable , Will be marked ; The second time is whether it is necessary to call finalize() Method filtering , Screening rules : Is it useful to rewrite finalize() Method . Second execution finalize() Method , It is determined that there is no need to call .
If you need to call , Then mark it for the second time , Then the object is recycled .
This method is strongly discouraged , The uncertainty is too great , The order of object calls cannot be guaranteed .
边栏推荐
- Linux下安装redis 、Windows下安装redis(超详细图文教程)
- rman備份報ORA-19809 ORA-19804
- 1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation
- How to implement two factor authentication MFA based on RADIUS protocol?
- Basic content learning of software testing (I)
- Machine virtuelle 14 installer win7 (tutoriel)
- 1. Kimball dimension modeling of data warehouse: what is a fact table?
- Learn how Alibaba manages the data indicator system
- Full link service tracking implementation scheme
- Expérience d'optimisation SQL: de 30248 secondes à 0001 secondes
猜你喜欢

Write a simple timeline

DEJA_ Vu3d - 051 of cesium function set - perfect realization of terrain excavation

布隆过滤器 课程研究报告

Music website design based on harmonyos (portal page)

The Cassandra cluster reinstalls and starts from the node. An error is reported. There is an existing solution

SQL optimization experience: from 30248 seconds to 0.001 seconds

Ingersoll Rand面板维修IR英格索兰微电脑控制器维修XE-145M
![[ybtoj advanced training guidance] class roll call [string hash]](/img/5b/bbf8fa51d180b50fbbee4bcc278c70.jpg)
[ybtoj advanced training guidance] class roll call [string hash]

Decision table method for basic content learning of software testing (2)

Stock suspension
随机推荐
DolphinScheduler使用系统时间
[ybtoj advanced training guide] maximum separation [hash] [Floyd]
Dolphin scheduler uses system time
PMP考试重点总结六——图表整理
虚拟机14安装win7(图教程)
[ybtoj advanced training guidance] class roll call [string hash]
Android studio interview preparation
多线程-并发并行-线程进程
From knowledge to wisdom: how far will the knowledge map go?
买卖股票费用计算
Differences between task parameter types inout and ref
Data mining modeling practice
微信小程序开发日志
Stock suspension
1181:整数奇偶排序
Use of Jasper soft studio report tool and solution of thorny problems
基于宽表的数据建模
异常处理4种方法
買賣股票費用計算
1180: fractional line delimitation /p1068 [noip2009 popularization group] fractional line delimitation