1 变量的初始化
1.1 变量的【定义】
1.2 变量的【作用域】
1.3 变量的【初始值】
1.4 补充:缓存变量
1.5 变量的【分类】与【未初始化情况】
2 类对象
2.1 类对象的初始化/构造过程
- 1 【对象】的初始化流程
- 2 实验
class Base{
static { System.out.println("[Base:static area]"); }//step1
{ System.out.println("Base:instance area"); }//step3
Base(){ System.out.println("<Base:constructor>"); }//step4
}
public class Create extends Base{
static { System.out.println("[Create:static area]"); }//step2
{ System.out.println("Create:instance area"); }//step5
Create(){//step6
// super();//默认:首先调用super()
System.out.println("<Create:constructor>");
}
public static void main(String args[]){ Create create = new Create(); }
}
//Output
[Base:static area]
[Create:static area]
Base:instance area
<Base:constructor>
Create:instance area
<Create:constructor>
- 3 360公司2020春招技术C卷试题
(题目小有改动)
class Clazz {
public static int x;
public boolean z;
public static String printX(Clazz clazz) { return "x:" + clazz.x; }
public static String printZ(Clazz clazz){ return "z:" + clazz.z; }
}
public class Singleton {
private static Singleton singleton = new Singleton();
public int counter0;//[值域/类的属性 →基本类型变量] 默认 初始化为0
public static int counter1;//[值域/类的属性] 默认 初始化为0
public static int counter2 = 0;//[值域/类的属性]
public Clazz clazz;//[值域/类的属性 → 引用类型变量]
private Singleton() {
int x;//[局部变量]
// System.out.println(x);//局部变量未初始化 ∴编译时报错 [局部变量]
System.out.println(Clazz.printX(clazz));//x:0 /[值域/类的属性 → 引用类型变量 de 静态属性]
System.out.println(Clazz.printZ(clazz));//运行时报错 (ExceptionInInitializerError) [值域/类的属性 → 引用类型变量 de 非静态属性]
counter0++;
counter1++;
counter2++;
}
public static Singleton getInstance(){ return singleton; }
public static void main(String[] args) {
Singleton singleton = Singleton.getInstance();
System.out.println(singleton.counter0);//1
System.out.println(Singleton.counter1);//1
System.out.println(Singleton.counter2);//0
}
}
1
1
0
原文地址:https://www.cnblogs.com/johnnyzen/p/12586027.html
时间: 2024-10-09 16:13:21