TJI读书笔记07-初始化

Posted 吾码2016

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TJI读书笔记07-初始化相关的知识,希望对你有一定的参考价值。

 

成员初始化

java尽量去保证每个变量在使用前都会得到初始化.

  • 对于方法局部变量,java不会自动初始化他们,如果没有显式的初始化,编译器会报错.
  • 对于类的数据成员,java会自动初始化成一个”空”“的值.简单来说,这个空的值对于基本数据类型就是,0,false,和空格. 对于引用类型就是null.

(只是类的数据成员会隐式初始化,这个我之前还真没有太在意,一直以为java默认的初始化是对所有类型的变量都生效的. )

显式初始化就不用说了. 很简单.

构造方法初始化

在构造方法中初始化变量是个很灵活的办法.但是,成员变量的隐式初始化是不会被阻止的.

public class Counter{
int i;
Counter(){
i=7;
}
}

这段代码,i会先被初始化为0,然后在构造方法中在被初始化为7;

初始化块

  • static关键字不能作用于局部变量.
  • 无论创建多少个对象,静态数据在内存中只占用一份存储区域.(static修饰的玩意儿都是属于类而不是属于实例的.)
  • static修饰的成员变量会被自动初始化为”空”.

一码解千愁,把这段代码放到eclipse中debug一下,就全明白了.

class Bowl{
Bowl(int marker){
System.out.println("Bowl("+marker+")");
}
public void f1(int marker){
System.out.println("f1("+marker+")");
}
}

class Table{
static Bowl bowl1 = new Bowl(1);
Table(){
System.out.println("Table()");
bowl2.f1(1);
}
void f2(int marker){
System.out.println("f2("+marker+")");
}
static Bowl bowl2 = new Bowl(2);
}

class CupBoard{
Bowl bowl3 = new Bowl(3);
static Bowl bowl4 = new Bowl(4);
CupBoard() {
System.out.println("CupBoard()");
bowl4.f1(2);
}
void f3(int marker){
System.out.println("f3("+marker+")");
}
static Bowl bowl5 = new Bowl(5);
}

public class StaticInitialization {
public static void main(String[] args) {
System.out.println("Create new CupBoard() in main");
new CupBoard();
System.out.println("Create new CupBoard() in main");
new CupBoard();
table.f2(1);
cupboard.f3(1);
}
static Table table = new Table();
static CupBoard cupboard = new CupBoard();
}
/*
Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
CupBoard()
f1(2)
Create new CupBoard() in main
Bowl(3)
CupBoard()
f1(2)
Create new CupBoard() in main
Bowl(3)
CupBoard()
f1(2)
f2(1)
f3(1)
*/

  • static成员变量初始化会在所有语句之前执行,并且只执行一次
  • 非static成员变量初始化紧随static成员变量初始化语句之后,每次加载类的时候都会执行
  • 成员变量初始化之后,执行构造函数
  • 构造函数执行完,才回再去执行其他函数.

埃大爷的总结显得更高端:
假设有一个Dog类

  • 即使没有显式的static关键字,构造器实际上也是一个static方法.首次创建Dog类的对象的时候,或者Dog类的静态方法,静态字段首次被访问的时候,java解释器会查找类的路径,定位Dog.class文件
  • 然后装载class文件,有关静态初始化的所有动作在这个时候就会执行. 因此,静态初始化只在类首次加载的时候执行一次
  • 当new Dog()的时候,先在堆上分配为Dog对象分配足够的空间
  • 然后这块空间清零,这就将Dog对象中的所有基本类型数据都设置成了”空”值
  • 执行所有字段定义处的初始化动作
  • 执行构造器

所有的成员变量初始化的语句都可以看作一个只有一条语句的初始化块. 那么带static关键字的可以看作静态初始化块,不带static关键字的,可以看作非静态初始化块.

static{
int i=0;
}

{
int j=0;
}

做个反向推论,既然单条语句的特殊的初始化块适用于埃大爷的原则,那么普通的初始化块也适用这个原则(这是多么无耻不负责任的推论…)

初始化的顺序

初始化顺序其实在上面都说完了. 为什么是这样的埃大爷也解释了. 小白总结就是:

  • 默认的初始化
  • 显式初始化语句和初始化块
  • 构造器中
  • 通过方法的赋值

按照埃大爷的解释,那静态初始化实在类装载的时候,而默认的初始化实在为实例分配空间的时候,那静态初始化块应该是早于默认初始化的. 不确定对不对,先记者,回头看到了再说.

%23TJI%u8BFB%u4E66%u7B14%u8BB007-%u521D%u59CB%u5316%[email protected]%28034.java%29%5Bjava%7Cjava%u7F16%u7A0B%u601D%u60F3%5D%0A%0A%5BTOC%5D%0A%0A%23%23%u6210%u5458%u521D%u59CB%u5316%0Ajava%u5C3D%u91CF%u53BB%u4FDD%u8BC1%u6BCF%u4E2A%u53D8%u91CF%u5728%u4F7F%u7528%u524D%u90FD%u4F1A%u5F97%u5230%u521D%u59CB%u5316.%20%0A-%20%u5BF9%u4E8E%u65B9%u6CD5%u5C40%u90E8%u53D8%u91CF%2Cjava%u4E0D%u4F1A%u81EA%u52A8%u521D%u59CB%u5316%u4ED6%u4EEC%2C%u5982%u679C%u6CA1%u6709%u663E%u5F0F%u7684%u521D%u59CB%u5316%2C%u7F16%u8BD1%u5668%u4F1A%u62A5%u9519.%20%0A-%20%u5BF9%u4E8E%u7C7B%u7684%u6570%u636E%u6210%u5458%2Cjava%u4F1A%u81EA%u52A8%u521D%u59CB%u5316%u6210%u4E00%u4E2A%22%u7A7A%22%22%u7684%u503C.%u7B80%u5355%u6765%u8BF4%2C%u8FD9%u4E2A%u7A7A%u7684%u503C%u5BF9%u4E8E%u57FA%u672C%u6570%u636E%u7C7B%u578B%u5C31%u662F%2C0%2Cfalse%2C%u548C%u7A7A%u683C.%20%u5BF9%u4E8E%u5F15%u7528%u7C7B%u578B%u5C31%u662Fnull.%0A%0A%28%u53EA%u662F%u7C7B%u7684%u6570%u636E%u6210%u5458%u4F1A%u9690%u5F0F%u521D%u59CB%u5316%2C%u8FD9%u4E2A%u6211%u4E4B%u524D%u8FD8%u771F%u6CA1%u6709%u592A%u5728%u610F%2C%u4E00%u76F4%u4EE5%u4E3Ajava%u9ED8%u8BA4%u7684%u521D%u59CB%u5316%u662F%u5BF9%u6240%u6709%u7C7B%u578B%u7684%u53D8%u91CF%u90FD%u751F%u6548%u7684.%20%29%0A%0A**%u663E%u5F0F%u521D%u59CB%u5316**%u5C31%u4E0D%u7528%u8BF4%u4E86.%20%u5F88%u7B80%u5355.%20%0A%0A%0A%23%23%u6784%u9020%u65B9%u6CD5%u521D%u59CB%u5316%0A%u5728%u6784%u9020%u65B9%u6CD5%u4E2D%u521D%u59CB%u5316%u53D8%u91CF%u662F%u4E2A%u5F88%u7075%u6D3B%u7684%u529E%u6CD5.%u4F46%u662F%2C%u6210%u5458%u53D8%u91CF%u7684%u9690%u5F0F%u521D%u59CB%u5316%u662F%u4E0D%u4F1A%u88AB%u963B%u6B62%u7684.%20%0A%60%60%60%0Apublic%20class%20Counter%7B%0A%09int%20i%3B%0A%09Counter%28%29%7B%0A%09%09i%3D7%3B%0A%09%7D%0A%7D%0A%60%60%60%0A%u8FD9%u6BB5%u4EE3%u7801%2Ci%u4F1A%u5148%u88AB%u521D%u59CB%u5316%u4E3A0%2C%u7136%u540E%u5728%u6784%u9020%u65B9%u6CD5%u4E2D%u5728%u88AB%u521D%u59CB%u5316%u4E3A7%3B%0A%0A%23%23%23%u521D%u59CB%u5316%u5757%0A%0A-%20static%u5173%u952E%u5B57%u4E0D%u80FD%u4F5C%u7528%u4E8E%u5C40%u90E8%u53D8%u91CF.%0A-%20%u65E0%u8BBA%u521B%u5EFA%u591A%u5C11%u4E2A%u5BF9%u8C61%2C%u9759%u6001%u6570%u636E%u5728%u5185%u5B58%u4E2D%u53EA%u5360%u7528%u4E00%u4EFD%u5B58%u50A8%u533A%u57DF.%28static%u4FEE%u9970%u7684%u73A9%u610F%u513F%u90FD%u662F%u5C5E%u4E8E%u7C7B%u800C%u4E0D%u662F%u5C5E%u4E8E%u5B9E%u4F8B%u7684.%29%0A-%20static%u4FEE%u9970%u7684%u6210%u5458%u53D8%u91CF%u4F1A%u88AB%u81EA%u52A8%u521D%u59CB%u5316%u4E3A%22%u7A7A%22.%0A%0A%u4E00%u7801%u89E3%u5343%u6101%2C%u628A%u8FD9%u6BB5%u4EE3%u7801%u653E%u5230eclipse%u4E2Ddebug%u4E00%u4E0B%2C%u5C31%u5168%u660E%u767D%u4E86.%20%0A%60%60%60java%0Aclass%20Bowl%7B%0A%09Bowl%28int%20marker%29%7B%0A%09%09System.out.println%28%22Bowl%28%22+marker+%22%29%22%29%3B%0A%09%7D%0A%09public%20void%20f1%28int%20marker%29%7B%0A%09%09System.out.println%28%22f1%28%22+marker+%22%29%22%29%3B%0A%09%7D%0A%7D%0A%0Aclass%20Table%7B%0A%09static%20Bowl%20bowl1%20%3D%20new%20Bowl%281%29%3B%0A%09Table%28%29%7B%0A%09%09System.out.println%28%22Table%28%29%22%29%3B%0A%09%09bowl2.f1%281%29%3B%0A%09%7D%0A%09void%20f2%28int%20marker%29%7B%0A%09%09System.out.println%28%22f2%28%22+marker+%22%29%22%29%3B%0A%09%7D%0A%09static%20Bowl%20bowl2%20%3D%20new%20Bowl%282%29%3B%0A%7D%0A%0Aclass%20CupBoard%7B%0A%09Bowl%20bowl3%20%3D%20new%20Bowl%283%29%3B%0A%09static%20Bowl%20bowl4%20%3D%20new%20Bowl%284%29%3B%0A%09CupBoard%28%29%20%7B%0A%09%09System.out.println%28%22CupBoard%28%29%22%29%3B%0A%09%09bowl4.f1%282%29%3B%0A%09%7D%0A%09void%20f3%28int%20marker%29%7B%0A%09%09System.out.println%28%22f3%28%22+marker+%22%29%22%29%3B%0A%09%7D%0A%09static%20Bowl%20bowl5%20%3D%20new%20Bowl%285%29%3B%0A%7D%0A%0Apublic%20class%20StaticInitialization%20%7B%0A%09public%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%09%09System.out.println%28%22Create%20new%20CupBoard%28%29%20in%20main%22%29%3B%0A%09%09new%20CupBoard%28%29%3B%0A%09%09System.out.println%28%22Create%20new%20CupBoard%28%29%20in%20main%22%29%3B%0A%09%09new%20CupBoard%28%29%3B%0A%09%09table.f2%281%29%3B%0A%09%09cupboard.f3%281%29%3B%0A%09%7D%0A%09static%20Table%20table%20%3D%20new%20Table%28%29%3B%0A%09static%20CupBoard%20cupboard%20%3D%20new%20CupBoard%28%29%3B%0A%7D%0A/*%0ABowl%281%29%0ABowl%282%29%0ATable%28%29%0Af1%281%29%0ABowl%284%29%0ABowl%285%29%0ABowl%283%29%0ACupBoard%28%29%0Af1%282%29%0ACreate%20new%20CupBoard%28%29%20in%20main%0ABowl%283%29%0ACupBoard%28%29%0Af1%282%29%0ACreate%20new%20CupBoard%28%29%20in%20main%0ABowl%283%29%0ACupBoard%28%29%0Af1%282%29%0Af2%281%29%0Af3%281%29%0A*/%0A%60%60%60%0A-%20static%u6210%u5458%u53D8%u91CF%u521D%u59CB%u5316%u4F1A%u5728%u6240%u6709%u8BED%u53E5%u4E4B%u524D%u6267%u884C%2C%u5E76%u4E14%u53EA%u6267%u884C%u4E00%u6B21%0A-%20%u975Estatic%u6210%u5458%u53D8%u91CF%u521D%u59CB%u5316%u7D27%u968Fstatic%u6210%u5458%u53D8%u91CF%u521D%u59CB%u5316%u8BED%u53E5%u4E4B%u540E%2C%u6BCF%u6B21%u52A0%u8F7D%u7C7B%u7684%u65F6%u5019%u90FD%u4F1A%u6267%u884C%0A-%20%u6210%u5458%u53D8%u91CF%u521D%u59CB%u5316%u4E4B%u540E%2C%u6267%u884C%u6784%u9020%u51FD%u6570%0A-%20%u6784%u9020%u51FD%u6570%u6267%u884C%u5B8C%2C%u624D%u56DE%u518D%u53BB%u6267%u884C%u5176%u4ED6%u51FD%u6570.%20%0A%0A%u57C3%u5927%u7237%u7684%u603B%u7ED3%u663E%u5F97%u66F4%u9AD8%u7AEF%3A%0A%u5047%u8BBE%u6709%u4E00%u4E2ADog%u7C7B%0A-%20%u5373%u4F7F%u6CA1%u6709%u663E%u5F0F%u7684static%u5173%u952E%u5B57%2C%u6784%u9020%u5668%u5B9E%u9645%u4E0A%u4E5F%u662F%u4E00%u4E2Astatic%u65B9%u6CD5.%u9996%u6B21%u521B%u5EFADog%u7C7B%u7684%u5BF9%u8C61%u7684%u65F6%u5019%2C%u6216%u8005Dog%u7C7B%u7684%u9759%u6001%u65B9%u6CD5%2C%u9759%u6001%u5B57%u6BB5%u9996%u6B21%u88AB%u8BBF%u95EE%u7684%u65F6%u5019%2Cjava%u89E3%u91CA%u5668%u4F1A%u67E5%u627E%u7C7B%u7684%u8DEF%u5F84%2C%u5B9A%u4F4DDog.class%u6587%u4EF6%0A-%20%u7136%u540E%u88C5%u8F7Dclass%u6587%u4EF6%2C%u6709%u5173%u9759%u6001%u521D%u59CB%u5316%u7684%u6240%u6709%u52A8%u4F5C%u5728%u8FD9%u4E2A%u65F6%u5019%u5C31%u4F1A%u6267%u884C.%20%u56E0%u6B64%2C%u9759%u6001%u521D%u59CB%u5316%u53EA%u5728%u7C7B%u9996%u6B21%u52A0%u8F7D%u7684%u65F6%u5019%u6267%u884C%u4E00%u6B21%0A-%20%u5F53new%20Dog%28%29%u7684%u65F6%u5019%2C%u5148%u5728%u5806%u4E0A%u5206%u914D%u4E3ADog%u5BF9%u8C61%u5206%u914D%u8DB3%u591F%u7684%u7A7A%u95F4%0A-%20%u7136%u540E%u8FD9%u5757%u7A7A%u95F4%u6E05%u96F6%2C%u8FD9%u5C31%u5C06Dog%u5BF9%u8C61%u4E2D%u7684%u6240%u6709%u57FA%u672C%u7C7B%u578B%u6570%u636E%u90FD%u8BBE%u7F6E%u6210%u4E86%22%u7A7A%22%u503C%0A-%20%u6267%u884C%u6240%u6709%u5B57%u6BB5%u5B9A%u4E49%u5904%u7684%u521D%u59CB%u5316%u52A8%u4F5C%0A-%20%u6267%u884C%u6784%u9020%u5668%0A%0A%u6240%u6709%u7684%u6210%u5458%u53D8%u91CF%u521D%u59CB%u5316%u7684%u8BED%u53E5%u90FD%u53EF%u4EE5%u770B%u4F5C%u4E00%u4E2A%u53EA%u6709%u4E00%u6761%u8BED%u53E5%u7684%u521D%u59CB%u5316%u5757.%20%u90A3%u4E48%u5E26static%u5173%u952E%u5B57%u7684%u53EF%u4EE5%u770B%u4F5C%u9759%u6001%u521D%u59CB%u5316%u5757%2C%u4E0D%u5E26static%u5173%u952E%u5B57%u7684%2C%u53EF%u4EE5%u770B%u4F5C%u975E%u9759%u6001%u521D%u59CB%u5316%u5757.%0A%60%60%60java%0Astatic%7B%0A%09int%20i%3D0%3B%0A%7D%0A%0A%7B%0A%09int%20j%3D0%3B%09%0A%7D%0A%60%60%60%0A%u505A%u4E2A%u53CD%u5411%u63A8%u8BBA%2C%u65E2%u7136%u5355%u6761%u8BED%u53E5%u7684%u7279%u6B8A%u7684%u521D%u59CB%u5316%u5757%u9002%u7528%u4E8E%u57C3%u5927%u7237%u7684%u539F%u5219%2C%u90A3%u4E48%u666E%u901A%u7684%u521D%u59CB%u5316%u5757%u4E5F%u9002%u7528%u8FD9%u4E2A%u539F%u5219%28%u8FD9%u662F%u591A%u4E48%u65E0%u803B%u4E0D%u8D1F%u8D23%u4EFB%u7684%u63A8%u8BBA...%29%0A%0A%0A%23%23%23%u521D%u59CB%u5316%u7684%u987A%u5E8F%0A%u521D%u59CB%u5316%u987A%u5E8F%u5176%u5B9E%u5728%u4E0A%u9762%u90FD%u8BF4%u5B8C%u4E86.%20%u4E3A%u4EC0%u4E48%u662F%u8FD9%u6837%u7684%u57C3%u5927%u7237%u4E5F%u89E3%u91CA%u4E86.%20%u5C0F%u767D%u603B%u7ED3%u5C31%u662F%3A%0A-%20%u9ED8%u8BA4%u7684%u521D%u59CB%u5316%0A-%20%u663E%u5F0F%u521D%u59CB%u5316%u8BED%u53E5%u548C%u521D%u59CB%u5316%u5757%0A-%20%u6784%u9020%u5668%u4E2D%0A-%20%u901A%u8FC7%u65B9%u6CD5%u7684%u8D4B%u503C%0A%0A%u6309%u7167%u57C3%u5927%u7237%u7684%u89E3%u91CA%2C%u90A3%u9759%u6001%u521D%u59CB%u5316%u5B9E%u5728%u7C7B%u88C5%u8F7D%u7684%u65F6%u5019%2C%u800C%u9ED8%u8BA4%u7684%u521D%u59CB%u5316%u5B9E%u5728%u4E3A%u5B9E%u4F8B%u5206%u914D%u7A7A%u95F4%u7684%u65F6%u5019%2C%u90A3%u9759%u6001%u521D%u59CB%u5316%u5757%u5E94%u8BE5%u662F%u65E9%u4E8E%u9ED8%u8BA4%u521D%u59CB%u5316%u7684.%20%u4E0D%u786E%u5B9A%u5BF9%u4E0D%u5BF9%2C%u5148%u8BB0%u8005%2C%u56DE%u5934%u770B%u5230%u4E86%u518D%u8BF4.%0A%0A%0A%0A

以上是关于TJI读书笔记07-初始化的主要内容,如果未能解决你的问题,请参考以下文章

TJI读书笔记09-访问控制权限

TJI读书笔记14-闭包与回调

TJI读书笔记15-持有对象

TJI读书笔记11-多态

TJI读书笔记12-接口

TJI读书笔记16-异常处理