forked from damaohongtu/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStaticTest.java
More file actions
65 lines (54 loc) · 1.26 KB
/
StaticTest.java
File metadata and controls
65 lines (54 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package JavaBasic.staticdemo;
/**
* @Author MaoTian
* @Classname StaticTest
* @Description static使用
*
* test static
* myclass static
* person static
* person Test
* test constructor
* person MyClass
* myclass constructor
*
* @Date 下午8:23 2019/8/12
* @Version 1.0
* @Created by mao<tianmao818@qq.com>
*/
class Person{
static{
System.out.println("person static");
}
public Person(String str) {
System.out.println("person "+str);
}
}
//StaticTest已经加载过了
//而在生成对象的时候,必须先初始化父类的成员变量,因此会执行Test中的Person person = new Person()
class MyClass extends StaticTest {
Person person = new Person("MyClass");
//(3)被加载
static{
System.out.println("myclass static");
}
public MyClass() {
System.out.println("myclass constructor");
}
}
//
public class StaticTest {
Person person = new Person("Test");
//(1)首先被加载
static{
System.out.println("test static");
}
public StaticTest() {
System.out.println("test constructor");
}
public static void main(String[] args) {
//(2)被执行
new MyClass();
}
OutOfMemoryError outOfMemoryError;
}