Java object initialization and cleanup __java

Source: Internet
Author: User

Java has an automatic garbage collector that does not require us to manually clean objects, but it requires us to create objects manually. So, for an object, its creation process or the initialization process, how we design our own class and how to ensure that the initialization of the object smoothly.

The basic initialization process is as follows:
-When you first create an object of type MyClass (a constructor can be viewed as a static method), or a static method (a static data field) that first invokes the MyClass class, the Java interpreter looks for the classpath and locates the Myclass.class file;
-Loads Myclass.class, creates a corresponding class object, executes all static initialization actions in this class, and static initialization occurs only once when the class object is first loaded, and if the object has a parent class, the above action is first performed;
-When you create a MyClass object with new, the MyClass object will be allocated enough storage space on the heap, and the storage space will be zeroed, which automatically sets all the base type data in the MyClass object to the default value, and the object reference is set to NULL; If this object has a parent class, Perform the above action first;
-Performs the initialization work in the order of the definition of the class member field, and then calls the constructor method, and if the object has a parent class, the action is performed first.

The different operations are described in detail below. initialization of non-inherited objects

Here is an instance of the generic class of objects, a class that is shaped like [modifier] class MyClass {}, and does not inherit any class (certainly inherited from the object class). The initialization of such class objects is divided into two cases-accessing static members of the Class (myclass.*) and creating a new object instance (new).
This defines a parent class as a test class:

Class Parent {public static final String PARENTSTATICFIELD1;
        static{System.out.println ("Before Parent ' s static initialization Block");
        System.out.println (Tostaticstring ());
        Parentstaticfield1= "PARENT";
        System.out.println ("After Parent ' s static initialization Block");
    System.out.println (Tostaticstring ());
    private static String parentstaticfield2=initstaticfield2 ();
    Protected String name= "";
    protected String ID;
        {System.out.println ("before Parent ' s instance initialization block 1");
        System.out.println (ToString ());
        old=30;
        System.out.println ("After Parent ' s instance initialization block 1");
    System.out.println (ToString ());
    } protected int old=40;
        {System.out.println ("before Parent ' s instance initialization block 2");
        System.out.println (ToString ());
        Name= "Parent";
        System.out.println ("After Parent ' s instance initialization block 2");
    System.out.println (ToString ());
        Public parent () {System.out.println ("before parent ' s construction method"); System.out.pRintln (ToString ());
        Id= "1001100110011001";
        System.out.println ("After Parent ' s construction method");
    System.out.println (ToString ());
        protected static String InitStaticField2 () {System.out.println ("parentStaticField2 initialization");
    return "parent"; public static String tostaticstring () {return "parentstaticfield1=" +parentstaticfield1+ ",
    Parentstaticfield2= "+PARENTSTATICFIELD2;
    @Override public String toString () {return "name=" +name+ ", id=" +id+ ", old=" +old "; }
}
accessing static members of a class

When you access only static members of a class, the initialization process is as follows:
-The Java interpreter finds the classpath, locates the. class file, finds the. class file after it is loaded, and creates a corresponding class object in the virtual machine;
-Executes the initialization process sequentially according to the order in which static data fields and static initialization blocks are defined in the class;
-The static initialization process for any class is performed only once.

        String S=parent.parentstaticfield1;
Output:
//Before parent ' s static initialization block
//parentstaticfield1=null, Parentstaticfield2=null
//after parent ' s Static initialization block
//parentstaticfield1=parent, Parentstaticfield2=null
//PARENTSTATICFIELD2 initialization
Create a new object instance

If you have already accessed a static member of this class before you create a new object instance, the static initialization process is not repeated, otherwise the static initialization process is performed, and then the following steps are performed:
-Allocate enough storage space on the heap for the class object, and the storage space is zeroed, which automatically sets all the base type data in the class object to the default value, and the object reference is set to null;
-Executes the initialization process sequentially according to the definition order of non-static data fields and non-static initialization blocks in the class;
-Invoke the corresponding construction method;
-Each time you create a new class object, the preceding steps are executed again.

        New Parent ();
Output:
//Before parent ' s static initialization block
//parentstaticfield1=null, Parentstaticfield2=null
//after parent ' s Static initialization block
//parentstaticfield1=parent, Parentstaticfield2=null
//PARENTSTATICFIELD2 initialization
//Before  Parent ' s instance initialization block 1
//Name=, Id=null, old=0
//After Parent ' s instance initialization block 1
//Name=, Id=null, old=30
//Before Parent ' s instance initialization block 2
//Name=, Id=null, old=40
//After Parent ' s instance initialization block 2
//Name=parent, Id=null, old=40
  
   //before Parent ' s construction method//
name=parent, Id=null, old=40
//After Parent ' s construction method
/name=parent, id=1001100 110011001, old=40
  

Or:

        String S=parent.parentstaticfield1;
        System.out.println ("New Parent object");
        New Parent ();
Output:
//Before parent ' s static initialization block
//parentstaticfield1=null, Parentstaticfield2=null
//after parent ' s Static initialization block
//parentstaticfield1=parent, Parentstaticfield2=null
//PARENTSTATICFIELD2 initialization
//New PARENT Object
//Before parent ' s instance initialization block 1
//Name=, Id=null, old=0
//After parent ' s instance initialization block 1
//Name=, Id=null, old=
before parent ' s instance initialization block 2
//Name=, Id=null, old=40
//After parent ' s instance initialization block 2
//Name=parent, id= NULL, old=40
//before parent ' s construction method/
/Name=parent, id=null, old=40
//After Parent ' s construction method
//Name=p Arent, id=1001100110011001, old=40
Inheriting object Initialization

Now consider the class object with the inheritance hierarchy, which is the class object (MyClass1 not the object class), which is shaped like [modifier] class MyClass2 extends MyClass1. Define the child class as the second Test class:

Class Child extends Parent {public static final String CHILDSTATICFIELD1;
        static{System.out.println ("Before child ' s static initialization Block");
        System.out.println (Tostaticstring ());
        childstaticfield1= "Child";
        System.out.println ("After the child ' s static initialization Block");
    System.out.println (Tostaticstring ());
    private static String childstaticfield2=initstaticfield2 ();
        {System.out.println ("before child ' s instance initialization block");
        System.out.println (ToString ());
        Name= "Child";
        old=15;
        System.out.println ("After the child ' s instance initialization block");
    System.out.println (ToString ());
        Public Child () {System.out.println ("before child ' s construction method");
        System.out.println (ToString ());
        Id= "0110011001100110";
        System.out.println ("After the child ' s construction method");
    System.out.println (ToString ());
        protected static String InitStaticField2 () {System.out.println ("childStaticField2 initialization");
    Return to "child";
   } public static String tostaticstring () {return "childstaticfield1=" +childstaticfield1+ ", CHILDST
    Aticfield2= "+CHILDSTATICFIELD2; }
}
to access a static member of a parent class

When only static members of the parent class are accessed, the static initialization of the parent class is performed only.

        String S=child.parentstaticfield1;
\ output:
\ Before parent ' s static initialization block
\ parentstaticfield1=null, Parentstaticfield2=null
\ After parent ' s Static initialization block
\ parentstaticfield1=parent, parentstaticfield2=null
\ parentStaticField2 Initialization
to access a static member of a child class

When only a static member of a subclass is accessed, the static initialization of the parent class is performed, and the static initialization of the subclass is executed.

        String S=child.childstaticfield1;
Output:
//Parent class static initialization process//
before child ' s static initialization block
//Childstaticfield1=null, Childstaticfield2=null
//After child ' s static initialization block
//Childstaticfield1=child, Childstaticfield2=null
//CHILDSTATICFIELD2 initialization
object instance of new subclass

If you have already accessed a static member of a parent class before you create an object instance of a subclass, the static initialization of the parent class is not repeated, and if a static member of the subclass has been accessed before the object instance of the subclass is created, the static initialization of the parent class and subclass is not repeated. After the static initialization process, you need to perform the following steps:
-Executes the non-static initialization process of the parent class and the method of constructing the parent class;
-Executes the non-static initialization process of subclasses and the construction methods of subclasses.

        String S=child.parentstaticfield1;
        System.out.println ("New child Object");
        New Child ();
Output:
//Parent class static initialization process//
new
child object//subclass static initialization process
before parent ' s instance initialization block 1
name=, Id=null, old=0< C9/>after Parent ' s instance initialization block 1
name=, Id=null, old=30
before parent ' s instance initialization block 2 name=, Id=null
,
old=40 After parent ' s instance initialization block 2
name=parent, Id=null, old=40
before Parent ' s construction method name=parent, Id=null
, old=40< C17/>after Parent ' s construction method
name=parent, id=1001100110011001, old=40
before child ' s instance initialization block
name=parent, id=1001100110011001, old=40 after the child
' s instance initialization block
Name=child, id=1001100110011001, old=15
before Child ' s construction method
Name=child, id=1001100110011001, old=15 after the child
' s construction method
Name=child, id= 0110011001100110, old=15

Or:

        String S=child.childstaticfield1;
        System.out.println ("New child Object");
        New Child ();
Output:
//Parent class static initialization process
//subclass static initialization process
new child object
//Parent class non-static initialization process and construction method
//subclass of Non-static initialization process and construction method

Or:

        New Child ();
Output:
//Parent class static initialization process//
subclass static initialization process
//parent class non-static initialization process and construction method
//subclass of Non-static initialization process and construction method
Object Cleanup

Java allows you to define a method named Finalize () in a class: Once the garbage collector is ready to release the storage space occupied by the object, the Finalize () method is called first and the memory occupied by the object is actually reclaimed when the next garbage collection action occurs.
Objects in Java are not always garbage collected: objects may not be garbage collected; garbage collection does not equal to "destructor"; garbage collection is only memory-related.
Neither "garbage collection" nor "end" is guaranteed to occur, and if the Java virtual machine is not running out of memory, it will not waste time doing garbage collection to recover memory.
System.GC () is used to force an end action.

Class Finalize {public-
    finalize () {}
    @Override public
    void Finalize () {
        System.out.println () Finalize ' s finalize ');
    }

"' Java
Finalize F=new finalize ();
F=null;
System.out.println ("After set F=null");
System.GC ();
System.out.println ("After Invoke System.GC ()");
Output
After set F=null
After Invoke System.GC ()
Finalize ' s finalize

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.