当前位置:网站首页>Classic cases of static keywords and block blocks
Classic cases of static keywords and block blocks
2022-07-25 06:11:00 【Eyes of years】
Static Key words and block Classic case of block
public class StaticTest02 {
public static int id;
// Class variables | Static variables ---> The static variables of the class are initialized in the static area of the method area id=0
public int age; // Member variables
public static void main(String[] args) {
StaticTest02 staticTest02 = new StaticTest02();
// Instantiation staticTest02 Make room in heap memory 0X001--> Member variables follow the object into heap memory for initialization age=0
staticTest02.age=10;// Object references are member variables age The assignment is 10
System.out.println(StaticTest02.id);
// Output the static variable value of the class id=0
System.out.println(staticTest02.age);
// Object calls member variables age Output is 10
staticTest02.change();
// Call the member method for the first time , Make room in heap memory 0x002-->id=1 age=10 //id=1 staticTest02.age=1 Method used , Stack frame release
System.out.println(StaticTest02.id); //1
System.out.println(staticTest02.id);
// The object here refers to 0X001---> therefore id yes 1
System.out.println(staticTest02.age);
// The object here refers to 0X001--> age=10
staticTest02.change();
// Second call change Method to open up heap space -->0X003 Print the first sentence :id=2 age=10( Here age Still 0X001 Of age value )
// Print the second sentence :id=2 staticTest02.age=1
System.out.println(id); //id=2
System.out.println(staticTest02.age);
//age=10
// When main After the method is executed, the stack frame is released
}
public void change() {
StaticTest02 staticTest02 = new StaticTest02();
// Instantiate class objects staticTest02, Member variables follow the object into heap memory for initialization age=0
staticTest02.age++;// Member variables of class objects age:1
StaticTest02.id++;// Of class variables of class objects id by 1
System.out.println("In Change(): id = " + id + " age = " + age);
// First call id=1 age=10
System.out.println("In Change(): id = " + id + " staticTest02.age = " + staticTest02.age);
// First call id=1 staticTest02.age=1
//id=2 staticTest02.age=10
}
}
1. block block {}-> Scope
{} Local code block | Ordinary statement block –> Method or statement block Execution opportunity : Method call
Define a piece of code with the same logic in a block , It's semantic , Self forming scope
2. {} Tectonic block --> Outside the method in the class Execution opportunity : new When you're with someone
Make some initial actions for member content
3. static{} A static block —> Class
Initialize static content
For some configurations … It only needs to be executed once , Subsequent reusable code can be defined in static blocks , Do some initial behavior , Do not encapsulate method calls
4. Synchronized block
Be careful :
static Member modifier Only member variables can be modified, not local
Local variables cannot be called externally , Because it exists in the stack frame of the method , Follow method calls , The building block code is executed before the constructor code , If there are multiple building blocks , From top to bottom
Initialize after the class is loaded for the first time , And only once
If there are multiple static blocks , The plex is executed from top to bottom .
Execution order : A static block -> Main method -> Tectonic block -> Constructors
/** The code of the building block will be compiled into the constructor code during compilation , Before the constructor code debug Debugging tools 1. Track the execution of the program 2. location , Debug the location where the exception occurs 3. Observe the changes of variables during program execution 4. Learn the source code and the execution process Use steps : 1) Breaking point When the program executes to the breakpoint , stop , The subsequent execution is controlled by the program ape Click the break point after the line number 2) debug Environment is running IDEA Common shortcut key : step over F8 Go on to the next step , If the next step is to call the method , Do not enter step into F7 Go on to the next step , If the next step is to call the method , How to get in , If the source code method does not enter Force step into alt+shift+F7 Go on to the next step , If the next step is to call the method , Forced entry method , If the source code method is forced to enter step out shift+f8 Call up to the location of the method call **/
public class BlockTest02 {
public static int a = 0;
{
a = 10;
System.out.println("3 、 Non static code block execution a=" + a); //10
}
static {
a = 6;
System.out.println("1 、 Static code block execution a=" + a); //6
}
public BlockTest02() {
this(a); //6
System.out.println("6、"+a); //10
System.out.println("7、 The parameterless construction method performs a=" + a); //10
}
public BlockTest02(int n) {
//n=6
System.out.println("4 、"+n); //6
System.out.println("5、"+a);// 10
}
public static void main(String[] args) {
System.out.println("2 、main"); //main
BlockTest02 tsc = new BlockTest02();
System.out.println(tsc);
}
}
边栏推荐
- Blocking Queue Analysis
- A little experience about von Mises distribution
- 日期(DAY 76)
- Context must be a dict rater solution
- Vbs script COM object extension and use (3)
- (牛客多校二)J-Link with Arithmetic Progression(最小二乘法/三分)
- Amazoncaptcha 95%成功率绕过亚马逊IP验证码
- ARM裸板调试之JTAG调试源码级调试
- 新时代生产力工具——FlowUs 息流全方位评测
- Era5 dataset description
猜你喜欢

剑指 Offer 36. 二叉搜索树与双向链表

(2022牛客多校)D-Link with Game Glitch(spfa)

node.express中req.body总是undefind解决

JTAG debugging source level debugging of arm bare board debugging

Sword finger offer 45. arrange the array into the smallest number

How to play a data mining game entry Edition

(Niuke multi School II) j-link with arithmetic progress (least square method / three points)

Promise implementation

EOL offline sequence based on iso13209 (Otx)

In depth analysis: is the hottest business model in 2022 linked by 2+1 a legal model?
随机推荐
Y76. Chapter IV Prometheus large factory monitoring system and practice -- Prometheus advanced (VII)
剑指 Offer 36. 二叉搜索树与双向链表
剑指 Offer 54. 二叉搜索树的第k大节点
(2022牛客多校二)K-Link with Bracket Sequence I(动态规划)
The macro definition of embedded C language development and the skills of finding the maximum value of two numbers
剑指 Offer 45. 把数组排成最小的数
The computer accesses the Internet normally with the same network cable, and the mobile phone connects to WiFi successfully, but it cannot access the Internet
HTB-Devel
Era5 dataset description
Codeforces Round #809 (Div. 2)
What does PK, NN, Qu, B, UN, ZF, AI, G mean when creating tables in MySQL
Unity model simplification / consolidation one click plug-in
What projects can make money online? Is it reliable to be we media?
"Everyday Mathematics" serial 61: March 1
target_compile_features specified unknown feature “cxx_std_14“ for target
Sword finger offer 54. the k-th node of the binary search tree
ERA5数据集说明
JTAG debugging source level debugging of arm bare board debugging
Run length test of R language: use the runs.test function to perform run length test on binary sequence data (check whether the sequence is random)
嵌入式c语言开发之宏定义求两个数的最大值的使用技巧