1.用構(gòu)造器確保初始化
- 以下為帶有構(gòu)造器的簡單類
class Rock{ Rock(){ System.out.println("Rock"); } } public class Test{ public static void main(String[] args){ Rock r = new Rock(); } }
在創(chuàng)建對象時,new Rock()將會為對象分配內(nèi)存惫撰,并調(diào)相應(yīng)的構(gòu)造器。不接受任何參數(shù)的構(gòu)造器是默認(rèn)構(gòu)造器。構(gòu)造器也可以帶參數(shù)撞蜂,以便于指定如何創(chuàng)建對象邀杏。
- 對默認(rèn)構(gòu)造器,如果你已經(jīng)創(chuàng)建了一個構(gòu)造器(無論是否有參數(shù))痴腌,編譯器就不會為你自動創(chuàng)建默認(rèn)構(gòu)造器真朗。例如
class Bird2{ Bird2 int }
2. this 關(guān)鍵字
this關(guān)鍵字表示對“調(diào)用方法的那個對象”的引用此疹。例如
class Leaf{ int i = 0; Leaf increment(){ i++; return this; } void print(){ System.out.println(i); } public static void main(String [] args){ Leaf l = new Leaf(); l.increment().increment().increment().print(); } }
this關(guān)鍵字將當(dāng)前對象傳遞給其他方法。
class Person{ public void eat(Apple apple){ Apple peeled = apple.getPeeled(); System.out.println("Yummy"); } } class Apple{ Apple getPeeled() { return Peeler.peel(this); } } class Peeler{ static Apple peel(Apple apple){ return apple; } } public class PassingThis { public static void main(String[] args) { new Person().eat(new Apple()); } }
Apple需要調(diào)用Peeler.peel()方法遮婶,它是一個外部的工具方法蝗碎,將執(zhí)行由于某種原因必須放在Apple外部的操作(可能是因為該外部方法要應(yīng)用于不同的類,而卻不想重復(fù)這段代碼)旗扑。為了將其自身傳遞給外部方法蹦骑,Apple必須使用this關(guān)鍵字。在構(gòu)造器中調(diào)用構(gòu)造器
public class Flower { int petalCount = 0; String s = "initial value"; Flower(int petals) { petalCount = petals; System.out.println(petalCount); } Flower(String ss){ s = ss; System.out.println(s); } Flower(int petals,String s){ this(petals); //this(s); this.s = s; System.out.println("String & int args"); } Flower() { this(5,"hi"); } void printPetalCount(){ //this(); System.out.println(petalCount+" "+ s); } public static void main(String[] args) { Flower f = new Flower(); f.printPetalCount(); } }
可以用this調(diào)用一個構(gòu)造器臀防,不能調(diào)用兩個脊串,另外,必須將構(gòu)造器調(diào)用置于最起始處清钥。除構(gòu)造器外,編譯器禁止其他任何方法中調(diào)用構(gòu)造器放闺。當(dāng)參數(shù)和數(shù)據(jù)成員名字相同時可以用this區(qū)分祟昭。
3.static
- static方法就是沒有this的方法。static方法中不能調(diào)用非靜態(tài)方法怖侦,反過來可以篡悟。可以在沒有創(chuàng)建任何對象的情況下 匾寝,通過類本身來調(diào)用static方法搬葬。它很像全局方法,java中禁止全局方法艳悔。
4.清理
java的垃圾回收器急凰;finalize()方法。
無論是垃圾回收還是終結(jié)猜年,都不保證一定會發(fā)生抡锈。如果java虛擬機(jī)(JVM)并未面臨內(nèi)存耗盡的情況,它是不會浪費時間去執(zhí)行垃圾回收以恢復(fù)內(nèi)存的乔外。
5. 初始化順序
在類的內(nèi)部床三,即使變量定義散布于方法定義之間,它們?nèi)匀粫谌魏畏椒ū徽{(diào)用之前得到初始化杨幼。
package com.zhangyue.learn;
class Window{
public Window(int maker) {
System.out.println("window"+maker);
}
}
class House{
public House() {
System.out.println("House");
w3 = new Window(1);
}
Window w2 = new Window(2);
public void f(){
System.out.println("f()");
}
Window w3 = new Window(3);
}
public class OrderOfInitialization {
public static void main(String[] args) {
House h = new House();
h.f();
}
}
/* output
window2
House
window1
f()
*/
6. 靜態(tài)數(shù)據(jù)的初始化
package com.zhangyue.learn;
class Bowl{
public Bowl(int marker) {
System.out.println("Bowl"+marker);
}
void f1(int marker){
System.out.println("f1"+marker);
}
}
class Table{
static Bowl b1 = new Bowl(1);
public Table() {
System.out.println("Table");
b2.f1(1);
}
void f2(int marker){
System.out.println("f2"+marker);
}
static Bowl b2 = new Bowl(2);
}
class Cupboard{
Bowl b3 = new Bowl(3);
static Bowl b4 = new Bowl(4);
public Cupboard() {
System.out.println("Cupboard");
b4.f1(2);
}
void f3(int marker){
System.out.println("f3"+marker);
}
static Bowl b5 = new Bowl(5);
}
public class StaticOrderOfInitialization {
public static void main(String[] args) {
System.out.println(1);
new Cupboard();
System.out.println(2);
new Cupboard();
t.f2(1);
// c.f3(1);
}
static Table t = new Table();
// static Cupboard c = new Cupboard();
/*Bowl1
Bowl2
Table
f11
1
Bowl4
Bowl5
Bowl3
Cupboard
f12
2
Bowl3
Cupboard
f12
f21
*/
}
靜態(tài)對象初始化過之后就不會再被初始化了撇簿,從第一個Cupboard和第二個Cupboard對象的初始化比較可看出聂渊。
初始化的順序是先靜態(tài)對象,后非靜態(tài)對象四瘫。
7. 數(shù)組的初始化
package com.zhangyue.learn;
public class DynamicArray {
public static void main(String[] args) {
Other.main(new String[]{"a","b","c"});
}
}
class Other{
static void main(String[] args){
for (String s : args) {
System.out.print(s+" ");
}
}
}
8. 可變參數(shù)列表
public class DynamicArray {
public static void printArray(Object ... c){
for (Object a : c) {
System.out.print(a);
}
}
public static void main(String[] args) {
printArray(1,2,3);
}
}
9. 枚舉類型
public enum Spiciness {
NOT,MILD,MEDIUM,HOT,FLAMING
}
創(chuàng)建enum時汉嗽,編譯器會自動添加一些有用的特性,如toString()莲组,以便于方便顯示實例內(nèi)容诊胞,還有ordinal(),表示順序锹杈,還有static values()撵孤,按順序構(gòu)成數(shù)組。
與switch連用顯示了它的特性
package com.zhangyue.learn;
public class SimpleEnumUse{
Spiciness degree;
public SimpleEnumUse(Spiciness degree){
this.degree = degree;
}
public void describe(){
System.out.println("aaaa");
switch(degree){
case NOT:System.out.println(1);break;
case MILD:System.out.println(2);break;
case MEDIUM:System.out.println(3);break;
case HOT:System.out.println(4);break;
case FLAMING:System.out.println(5);break;
default : System.out.println(0);
}
}
public static void main(String[] args){
SimpleEnumUse
s = new SimpleEnumUse(Spiciness.NOT),
m = new SimpleEnumUse(Spiciness.FLAMING),
n = new SimpleEnumUse(Spiciness.HOT);
s.describe();
m.describe();
n.describe();
}
}
/*aaaa
1
aaaa
5
aaaa
4
*/
10. 引用的初始化
編譯器不是為每個引用都創(chuàng)建默認(rèn)對象竭望,如果想初始化這些引用有下面幾種方法
package com.zhangyue.learn;
class Soap{
private String s;
//構(gòu)造器初始化
Soap() {
System.out.println("Soap()");
s = "Constructed";
}
public String toString(){
return s;
}
}
public class YinYongInitialization {
//在定義的位置初始化
private String
s1 = "Happy",
s2 = "Happy",
s3,s4;
private int i;
private Soap castille;
private float toy;
public YinYongInitialization(){
System.out.println("Inside class");
s3 = "Joy";
toy = 3.14f;
castille = new Soap();
}
//使用實例初始化
{i = 47;}
public String toString(){
//在正要使用對象之前初始化邪码,即惰性初始化
if (s4 == null) {
s4 = "Joy";
}
return s1+s2+s3+s4+toy+i+castille;
}
public static void main(String[] args) {
YinYongInitialization y = new YinYongInitialization();
System.out.println(y);
}
}