您的位置:首页 > 其它

第四章: Initialization & Cleanup

2005-12-07 18:29 351 查看
第四章: Initialization & Cleanup:1、Guaranteed initialization with the constructorthe coding style of making the first letter of all methods lowercase does not apply to constructors, since the name of the constructor must match the name of the class exactly
Tree t = new Tree(12);  // 12-foot tree
If Tree(int) is your only constructor, then the compiler won’t let you create a Tree object any other way2、Cleanup: finalization and garbage collectionWhen the garbage collector is ready to release the storage used for your object, it will first call finalize( ) --finalize函数不能直接调用,它用来释放一些特殊的不是用new产生的内存,因为垃圾回收器只知道回收由new分配的内存.这些特殊的不是用new产生的内存可能是因为java调用了非java程序的 方法引起的,所以用到finalize()的机会并不多.System.gc( ) is used to force finalizationStatic成员只会在需要的时侯初始化,之后就不会再作初始化了3、Order of initialization初始化顺利The variable definitions may be scattered throughout and in between method definitions, but the variables are initialized before any methods can be called—even the constructorIt’s helpful to summarize the process of creating an object. Consider a class called Dog:The first time an object of type Dog is created (the constructor is actually a static method), or the first time a static method or static field of class Dog is accessed, the Java interpreter must locate Dog.class, which it does by searching through the classpath.As Dog.class is loaded (creating a Class object), all of its static initializers are run. Thus, static initialization takes place only once, as the Class object is loaded for the first time.When you create a new Dog( ), the construction process for a Dog object first allocates enough storage for a Dog object on the heap.This storage is wiped to zero, automatically setting all the primitives in that Dog object to their default values (zero for numbers and the equivalent for boolean and char) and the references to null.Any initializations that occur at the point of field definition are executed.Constructors are executed.类的初始化顺序为:  (静态变量、静态初始化块)—— > (变量、初始化块)—— > 构造函数B类——extends——A类  创建子类B对象的顺序:  第一步:分配空间(A ,B的空间一次性分配)  第二步:执行(A ,B )的 静态代码块、静态变量 //二者根据调用出现的先后顺序执行  第三步:初始化A的实例变量,A的代码块 //二者根据调用出现的先后顺序执行  第四步:调用A的构造函数  第五步:初始化B的实例变量和代码块  第六步:调用B的构造函数4、Explicit static initialization(显示的静态初始化):Java allows you to group other static initializations inside a special “static clause” (sometimes called a static block) in a class. It looks like this:
class Spoon {
static int i;
static {
i = 47;
}
like other static initializations, is executed only once: the first time you make an object of that class or the first time you access a static member of that class (even if you never make an object of that class).5、Non-static instance initialization(非静态的实例初始化):
public class Mugs{
Mug c1;
{
c1 = new Mug(1);
System.out.println("c1 & c2 initialized");
}   //除了没有static关键字,和静态初始化没什么两样,在构造函数之前执行
Mugs() {
System.out.println("Mugs()");
} }
6、Array initializationstart counting from element zero, the largest element you can index is length – 1int[] a = new int[rand.nextInt(20)]; // an array of primitiveInteger[] a = new Integer[rand.nextInt(20)]; // an array of referencesint[] a1 = { 1, 2, 3, 4, 5 };
Integer[] a = {
new Integer(1),
new Integer(2),
new Integer(3),
};
print(new Object[] {
new Integer(47), new VarArgs(),
new Float(3.14), new Double(11.11) });//print函数,以组数为参数,能达到可变参数列表
7、Multidimensional arrays(多维数组)int[][] a1 = {{ 1, 2, 3, },{ 4, 5, 6, },    };that each vector in the arrays that make up the matrix can be of any length:
    int[][][] a3 = new int[rand.nextInt(7)][][];
    for(int i = 0; i < a3.length; i++) {
      a3[i] = new int[rand.nextInt(5)][];
      for(int j = 0; j < a3[i].length; j++)
        a3[i][j] = new int[rand.nextInt(5)];
}
    Integer[][] a4 = {
      { new Integer(1), new Integer(2)},
      { new Integer(3), new Integer(4)},
      { new Integer(5), new Integer(6)},
    };        // arrays of nonprimitive
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐