一.static 初始化
static 成员初始化顺序
package object;
class Bowl {
Bowl(int marker)
{
System.out.printf("Bowl("+marker+")
");
}
void f1(int marker)
{
System.out.printf("f1("+marker+")
");
}
}
class Table{
static Bowl bowl1 =new Bowl(1);
Table()
{
System.out.println("table()");
}
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.print("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("Creating new Cupboard() in main");
new Cupboard(); //静态成员只有在第一个Cupboard在创建时才会初始化,此后,静态成员不会再次初始化
System.out.println("Creating new Cupboard() in main");
Cupboard cupboard = new Cupboard();
new Table().f2(1);
new Cupboard().f3(1);
}
}/* output:
Creating new Cupboard() in main
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard
f1(2)
Bowl(1)
Bowl(2)
table()
f2(1)
Bowl(3)
Cupboard
f1(2)
f3(1)
*///~
显示的初始化静态方法
package object;
//: initialization/ExplicitStatic.java
import static net.mindview.util.Print.*;
class Cup
{
Cup(int marker)
{
print("Cup("+ marker +")");
}
void f(int marker)
{
print("f("+marker+")");
}
}
class Cups
{
static Cup cup1;
static Cup cup2; //静态域
static{
cup1 = new Cup(1);
cup2 = new Cup(2);
} //静态块
Cups()
{
print("Cups()");
}
}
public class ExplicitStatic{
public static void main(String args[])
{
print("Inside main()");
Cups.cup1.f(99); // (1) 无论时通过(1)还是注释掉(1)运行(2),Cups的静态初始化都会执行
// 静态初始化只会执行一次,如果(1)(2)全部注释掉则不会执行初始化
}
//static Cups cups1 = new Cups(); //(2)
//static Cups cups2 = new Cups(); //(2)
}/* output:
Inside main()
Cup(1)
Cup(2)
f(99)
*///~
二.非静态实例的初始化
package object; import static net.mindview.util.Print.*; class Mug { Mug(int marker) { print("mug("+ marker + ")"); } void f( int marker) { print("f("+marker +")"); } } public class Mugs{ Mug mug1; Mug mug2; { mug1 = new Mug(1); mug2 = new Mug(2); print("mug1 & mug2 initialized"); } //实例初始化子句是再构造器之前执行的 Mugs() { print("MUgs()"); } Mugs(int i){ print("Mugs(int)"); } public static void main(String args[]) { print("Instde main()"); new Mugs(); print("new Musg() cmopleted"); new Mugs(1); print("new Mugs(1) completed"); } }/* output: Instde main() mug(1) mug(2) mug1 & mug2 initialized MUgs() new Musg() cmopleted mug(1) mug(2) mug1 & mug2 initialized Mugs(int) new Mugs(1) completed *///~
三.数组的初始化
package object; import static net.mindview.util.Print.*; import java.util.*; public class ArrayNew{ public static void main(String args[]){ int [] a; Random rand = new Random(47); a = new int[rand.nextInt(20)]; print("length of a = " + a.length); print(Arrays.toString(a)); //Arrays.toString 产生一维数组的打印版本 } }/* output: length of a = 18 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]//数组元素会自动初始化为空值(对于数字和字符就是0,对于boolean是false) *///~
引用数组的初始化
package object; import static net.mindview.util.Print.*; import java.util.*; public class ArrayNew{ public static void main(String args[]){ Random rand = new Random(47); Integer [] a = new Integer[rand.nextInt(20)]; print("length of a = " + a.length); //for(int i = 0;i<a.length;i++) //a[i] = rand.nextInt(500); print(Arrays.toString(a)); } } /*如果创建一个非基本类型的数组,那么就创建了一个引用数组,直到把对象赋值给引用初始化才算完成 /**length of a = 18 [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null] *///~
用花括号初始化对象数组
package object; import static net.mindview.util.Print.*; import java.util.*; public class ArrayNew{ public static void main(String args[]){ Integer[] a={ new Integer(1), new Integer(2), 3,//Autoboxing }; Integer[] b= new Integer[]{ new Integer(1), new Integer(2), 3,//Autoboxing 最后一个逗号是可选的 }; System.out.println(Arrays.toString(a)); System.out.println(Arrays.toString(b)); } }/*output: [1, 2, 3] [1, 2, 3] *///~
package object; import static net.mindview.util.Print.*; import java.util.*; public class ArrayNew{ public static void main(String[] args) { Other.main(new String[]{ "fiddle", "de", "dum"});//传递给other ,用来替换命令行参数 } } class Other{ public static void main(String[] args) { for(String s : args) System.out.print(s + " "); } } /* output: fiddle de dum *///~
可变参数列表 旧版
package object; //: object/VarArgs.java /**这是Java SE5 之前的代码 class A {} public class VarArgs { static void printArray(Object[] args) //所有类都间接或直接继承自Object类 { for(Object obj : args) System.out.print(obj + " "); System.out.println(); } public static void main(String args[]) { printArray(new Object[]{ new Integer(47), new Float(3.14), new Double(11.11) }); printArray(new Object[]{"one", "two", "three"}); printArray(new Object[]{new A(),new A(), new A()});//未赋值默认打印类名和地址 } }/* output: 47 3.14 11.11 one two three object.A@41975e01 object.A@c2e1f26 object.A@dcf3e99 *///~
可变参数列表 新版
package object; /** this is new code * */ class A {} public class NewVarArgs { static void printArray(Object... args) //这里的Object 可以换成String Integer Character int char 等其它类型 { for(Object obj : args) System.out.print(obj + " "); System.out.println(); } public static void main(String args[]) { //can take individual elements: printArray(new Object[]{ new Integer(47), new Float(3.14), new Double(11.11) }); printArray(new Object[]{"one", "two", "three"}); printArray(new Object[]{new A(),new A(), new A()});//未赋值默认打印类名和地址 // or an array printArray((Object[])new Integer[]{1,2,3,4}); //因为Integer[] 已经是数组,所以不会执行任何转换 printArray();//Empty list is ok } }/* output: 447 3.14 11.11 one two three object.A@41975e01 object.A@c2e1f26 object.A@dcf3e99 1 2 3 4 *///~
以下是初始化的三种情况:
package object; //: reusing/Bath.java // commposition for code reuse class Soap { private String s; Soap() { System.out.println("Soap()"); s = "Constructed"; } public String toString(){return s;} } public class Bath{ private String //initializing at point of definition s1 = "Happy", //在定义对象的地方初始化 s2 = "Happy", s3,s4; private Soap castille; private int i; private float toy; public Bath() //在类的构造器中初始化 { System.out.println("Inside Bath()"); s3 = "joy"; toy = 3.14f; castille = new Soap(); } //Instance initialization {i=47;} //实例初始化 public String toString(){ if(s4 == null) //在使用之前 s4 = "joy"; return "s1 = " + s1 + " "+ "s2 = " + s2 + " "+ "s3 = " + s3 + " "+ "s4 = " + s4 + " "+ "i = " + i + " "+ "toy = " + toy + " " + "castille = " + castille; //在这里编译器知道你要将一个String对象("source =") //同WaterSource相加 } public static void main(String[] args) { Bath bath = new Bath(); System.out.println(bath);//当只有一个对象时会自动调用toString() } }/* output: Inside Bath() Soap() s1 = Happy s2 = Happy s3 = joy s4 = joy i = 47 toy = 3.14 castille = Constructed *///~