1.用構造器確保初始化
- 以下為帶有構造器的簡單類
class Rock{ Rock(){ System.out.println("Rock"); } } public class Test{ public static void main(String[] args){ Rock r = new Rock(); } }
在創建對象時,new Rock()將會為對象分配內存,并調相應的構造器。不接受任何參數的構造器是默認構造器。構造器也可以帶參數,以便于指定如何創建對象。
- 對默認構造器,如果你已經創建了一個構造器(無論是否有參數),編譯器就不會為你自動創建默認構造器。例如
class Bird2{ Bird2 int }
2. this 關鍵字
this關鍵字表示對“調用方法的那個對象”的引用。例如
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關鍵字將當前對象傳遞給其他方法。
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需要調用Peeler.peel()方法,它是一個外部的工具方法,將執行由于某種原因必須放在Apple外部的操作(可能是因為該外部方法要應用于不同的類,而卻不想重復這段代碼)。為了將其自身傳遞給外部方法,Apple必須使用this關鍵字。在構造器中調用構造器
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調用一個構造器,不能調用兩個,另外,必須將構造器調用置于最起始處。除構造器外,編譯器禁止其他任何方法中調用構造器。當參數和數據成員名字相同時可以用this區分。
3.static
- static方法就是沒有this的方法。static方法中不能調用非靜態方法,反過來可以。可以在沒有創建任何對象的情況下 ,通過類本身來調用static方法。它很像全局方法,java中禁止全局方法。
4.清理
java的垃圾回收器;finalize()方法。
無論是垃圾回收還是終結,都不保證一定會發生。如果java虛擬機(JVM)并未面臨內存耗盡的情況,它是不會浪費時間去執行垃圾回收以恢復內存的。
5. 初始化順序
在類的內部,即使變量定義散布于方法定義之間,它們仍然會在任何方法被調用之前得到初始化。
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. 靜態數據的初始化
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
*/
}
靜態對象初始化過之后就不會再被初始化了,從第一個Cupboard和第二個Cupboard對象的初始化比較可看出。
初始化的順序是先靜態對象,后非靜態對象。
7. 數組的初始化
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. 可變參數列表
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
}
創建enum時,編譯器會自動添加一些有用的特性,如toString(),以便于方便顯示實例內容,還有ordinal(),表示順序,還有static values(),按順序構成數組。
與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. 引用的初始化
編譯器不是為每個引用都創建默認對象,如果想初始化這些引用有下面幾種方法
package com.zhangyue.learn;
class Soap{
private String s;
//構造器初始化
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);
}
}