当前位置:网站首页>024.static and final use traps continued
024.static and final use traps continued
2022-07-19 11:40:00 【kfdbes】
Lesson 24
- about final Type member variable , Generally speaking, there are two ways to assign initial values :
- In a statement final The initial value is assigned when the member variable of type .
package exercise;
public class exercise009 {
final int a = 0;
}
2. In a statement final Member variables of type are not assigned initial values , But in all construction methods of a class, it is assigned an initial value .
package exercise;
public class exercise009 {
final int a;
public exercise009(){
a = 0;
}
public exercise009(int a){
this.a = a;
}
}
- static Code block : Static code block . The function of static code block is to complete some initialization . First execute the static code block , Then execute the constructor . Static code blocks are executed when the class is loaded , The construction method is executed when generating objects ; To call a class to generate an object , First, you need to load the class into Java Virtual machine (JVM), Then from jvm Load this class to generate objects .
package exercise;
public class exercise009 {
public static void main (String[] args){
P p = new P();
P p2 = new P();
}
}
class P{
static {
System.out.println("static block");
}
public P(){
System.out.println("P out");
}
}

package exercise;
public class exercise009 {
public static void main (String[] args){
new S();
}
}
class P{
static {
System.out.println("P static block");
}
public P(){
System.out.println("P out");
}
}
class Q extends P{
static {
System.out.println("Q static");
}
public Q(){
System.out.println("Q out");
}
}
class S extends Q{
static {
System.out.println("S static");
}
public S(){
System.out.println("S out");
}
}

3. The static code block of the class will be executed only once , It is executed when the class is loaded , Because each class will only be loaded once , All static code blocks will be executed only once ; The construction method is not , Every time an object is generated, the constructor of the class will be called , therefore new The constructor will be called once .
4. If there is an existing construction method in the inheritance system , There are also static code blocks , Then first execute the static code block of the topmost class , Execute all the way to the static code block of the lowest class . Be careful : Static code blocks are executed only once .
5. Non static member variables cannot be accessed in static methods ; You can access static member variables in static methods . Static member variables can be accessed in non static methods .
package exercise;
public class exercise010 {
public static void main (String[] args){
W.change();
}
}
class W {
static int a = 10;
public static void change(){
a++;
}
}
- summary : Static can only access static ; Non static access to everything
- Cannot be used in static methods this keyword .
边栏推荐
- Dream CMS foreground search SQL injection
- Ten minutes from pytorch to mxnet
- Unity high version returned low version error
- Leetcode 1304. N different integers with zero and
- Leetcode 1252. 奇数值单元格的数目
- Download of common getshell tools
- TS solves the problem that the type file of the imported plug-in does not exist
- TiKV 线程池性能调优
- 【嵌入式单元测试】C语言单元测试框架搭建
- A curated list of awesome Qt and QML
猜你喜欢

热议:老公今年已经34周岁想读博,以后做科研,怎么办?

机器人开发--常用仿真软件工具

Delegate parents and other loaders

《MySQL DBA封神打怪之路》专栏学习大纲

Leetcode 1252. 奇数值单元格的数目

Redis分布式缓存-Redis集群
![[binomial tree] the power of the button cattle customers must brush questions](/img/c0/254c28c5d94635e817fa6ba0b8483a.png)
[binomial tree] the power of the button cattle customers must brush questions

Configure spectrum navigation for Huawei wireless devices

Unity high version returned low version error

Unity高版本退回低版本报错问题
随机推荐
zabbix代理服务器配置
Transport layer -------- TCP (I)
The basic establishment of the sequence table and the related operations of adding, deleting, modifying and querying (the sequence table described in C language)
Unchangeable status quo
Leetcode 1252. Number of odd value cells
565. 数组嵌套 : 常规模拟题
Detailed explanation of MySQL show processlist
LeetCode刷题——查找和最小的 K 对数字#373#Medium
Antd form setting array fields
03-1. Inline function, auto keyword, typeID, nullptr
学习笔记3--规划控制中的机器学习基本思想
QT -- excellent open source project
Robot development -- common simulation software tools
02-2、缺省参数、函数重载、引用、隐式类型转换、关于报错
What do you look at after climbing the wall? The most popular foreign website - website navigation over the wall
Play with the one-stop scheme of cann target detection and recognition
【多线程】JUC详解 (Callable接口、RenntrantLock、Semaphore、CountDownLatch) 、线程安全集合类面试题
[binomial tree] the power of the button cattle customers must brush questions
Cv02 Roge matrix, rotation vector, angle
At5147-[agc036d]negative cycle [DP, model conversion]