Java类的初始化与垃圾清理

Member initialization

Inside class(类), primitives(原始类型) are given default values if you don’t specify values, If you haven’t given “i” an initial value and you try to use it anyway, you’ll get a runtime error called an exception.

void f() {  
    // Error -- the local variable i may not initialized   
    int i;     
    i++;  

    int [] a, b;
    // that's right, array 'a' has been initialized
    a = new int[5];
    // wrong, since 'b' is an array
    b = 2; 
    // right, its result is default 0
    System.out.print(a[0]);
} 
class Data {     
    int i = 999; 
    // Defaults to zero    
    long l;
} 
Array initialization
int a1[];  
int[] a1;   // is the same as this! 
int a[] = new int[2];
System.out.println(a[0]);// the result is 0
Constructor initialization

Order of initialization – Order that variables/objects are defined in class.
Let’s see.

1、

class Counter {   
    int i;   
    // i will first be initialized to 0, then to 7  
    Counter() 
    {   
        i = 7; 
    }
}

2、

class Card {   
    // Before constructor 1
    Tag t1 = new Tag(1); 
    // Then constructor 4   
    Card() {     
        // Indicate we're in the constructor:     
        System.out.println("Card()");  
        // Reinitialize t3   
        t3 = new Tag(33);    
    }   
    // Before constructor 2
    Tag t2 = new Tag(2); 
    // At last, method   
    void f() {      
        System.out.println("f()");   
    }
    // Before constructor 3
    Tag t3 = new Tag(3); 
} 
order of the execution:
1   Tag(1) 
2   Tag(2) 
3   Tag(3) 
4   Card() 
5   Tag(33) 
6   f()
Process of creating an object – class Dog

1、The first time an object of type Dog is created, or the first time a static method or static field of class Dog is accessed.
2、As Dog.class is loaded, all of its static initializers are run.
3、The construction process for a Dog object first allocates enough storage for a Dog object on the heap.
4、This storage is wiped to zero, automatically setting all the primitives in that Dog object to their default values.
5、Any initializations that occur at the point of field definition are executed.
6、Constructors are executed.

Explicit static initialization
class Cup {   
    Cup(int marker) {     
        System.out.println("Cup(" + marker + ")");   
    }   
    void f(int marker) {     
        System.out.println("f(" + marker + ")");   
    } 
} 
class Cups {   
    static Cup c1;   
    static Cup c2;   
    static {     
        c1 = new Cup(1);     
        c2 = new Cup(2);   
    }   
    Cups() {     
        System.out.println("Cups()");   
    } 
} 
public class ExplicitStatic {   
    public static void main(String[] args) {     
        System.out.println("Inside main()");     
        Cups.c1.f(99);     
    }  
    //* 
    //static Cups x = new Cups();    
    //static Cups y = new Cups();  
}
The order of execution:
1   Inside 
2   main() 
3   Cup(1) 
4   Cup(2) 
5   f(99) 
The order of execution after canceling the comment at *
1   Cup(1)
2   Cup(2)
3   Cups()
4   Cups()
5   Inside main()
6   f(99)
Order of constructor calls
  1. The base-class constructor is called. This step is repeated recursively such that the very root of the hierarchy is constructed first, followed by the next-derived class, etc., until the most-derived class is reached. (追溯到基类先构造,然后一层一层向下构造子类)
  2. Member initializers are called in the order of declaration. (成员的初始化按声明顺序从上到下)
  3. The body of the derived-class constructor is called. (最后是构造器)
Conclusions:

Java allows you to group other static initializations inside a special “static clause” (sometimes called a static block) in a class. like other static initializations, is executed only once.

Cleanup

Cleanup: Finalization and Garbage Collection

Important facts about garbage collection

1、Garbage collection is not destruction
2、Your objects may not get garbage collected
3、Garbage collection is only about memory

What is finalize() for?

1、In theory: releasing memory that the GC wouldn’t
2、It’s never been reliable: promises to be called on system exit; (causes bug in Java file closing)
3、Using finalize() to detect an object that hasn’t been properly cleaned up
4、finalize() is only useful for obscure memory cleanup that most programmers will never use

You must perform cleanup

1、Must write specific cleanup method

ps:

Java GC releases memory only: any other cleanup must be done explicitly!

猜你喜欢

转载自blog.csdn.net/qq_38232598/article/details/80630520