Java Learning Note II: Initialization (II)

Source: Internet
Author: User

Here the main learning initialization, divided into:

1. Variable initialization

2. Constructor initialization

3. Array initialization

4. Inheritance Initialization

1. Variable initialization

Member initialization is divided into two types:

(1) member variable initialization

When defining a member variable, the system gives a default value if the variable is not assigned to the initial value.

Here are two options: either use the default initial value of the system or define the initial value yourself.

The default output values given by the system are:

Boolean    false Char        []byte       0short      0int        0long       0float       0.0double     0.0refenece   null

Char has an initial value of 0, appears blank, and the String reference type is initially null by default

(2) Initialization of local variables

Local variables must be the merchant's initial value when using "basic type", or the compiler will make an error.

2. Constructor initialization

(1) member variables are initialized before any of the constructor calls, such as:

class init2{    privateint  i;          Public Init2 (int  i)    {        this. i = i;    }           Public Static void Main (string[] args)    {        new Init2 (3);    }         Private String Reference;}

1). I was given the initial value of 0 before the constructor call, and then the new value 3 was assigned in the constructor.

2). It is possible to initialize a member variable regardless of where it is scattered.

3). The order in which the variables are defined determines the order in which they are initialized.

(2) Initialization of a static database

Here are examples of "Java Programming ideas"

classbowl{ PublicBowl (intmaker) {        //Maker for markingSystem.out.println ("Bowl (" + maker + ")"); }         Public voidF1 (intmaker) {System.out.println ("F1 (" + maker + ")"); }}classtable{StaticBowl BOWL1 =NewBowl (1);//Static member variables         PublicTable () {System.out.println ("Table"); BOWL1.F1 (1); }         Public voidF2 (intmaker) {System.out.println ("F2 (" + maker + ")"); }        StaticBowl BOWL2 =NewBowl (2);//Static member variables}classcupboard{Bowl bowl3=NewBowl (3);//non-static member variable    StaticBowl Bowl4 =NewBowl (4);//Static member variables         Publiccupboard () {System.out.println ("Table"); BOWL3.F1 (2); }         Public voidF3 (intmaker) {System.out.println ("F3 (" + Maker + ")"); }        StaticBowl BOWL5 =NewBowl (5);//Static member variables}classinit3{ Public Static voidMain (string[] args) {System.out.println ("Init3.main ()"); Newcupboard (); System.out.println ("Init3.main ()"); Newcupboard (); TABLE.F2 (1); CUPBOARD.F3 (1); }        StaticTable Table =NewTable (); StaticCupboard cupboard =Newcupboard ();}

Execution Result:

Bowl (1) Bowl (2) Tablef1 (1) Bowl (4) Bowl (5) Bowl (3) Cupboardf1 ( 2) Init3.main () Bowl (3) Cupboardf1 (2) Init3.main () Bowl (3) Cupboardf1 (2) F2 ( 1) F3 (1)

Description

First step: Initialize static table table = new table () (because there is a static variable in the call Init3 main)

1.1. Initialize the static Bowl bowl1 = new Bowl (1) in the table, and the result of the output is: Bowl (1)

1.2. Initialize the static Bowl bowl2 = new Bowl (2) in the table, and the result of the output is: Bowl (2)

1.3. Initialize the table constructor, output: Table, and execute the BOWL1.F1 (1) method, output:F1 (1)

So after execution completes the static table table = new table (), the output is:

Bowl (1) Bowl (2) Tablef1 (1)

Step Two: Initialize the static cupboard cupboard = new cupboard ();

2.1. Initializing cupboard static Bowl bowl4 = new Bowl (4); At this point, the output is: Bowl (4)

2.2. Initializing in Cupboard static Bowl bowl5 = new Bowl (5); At this point, the output is: Bowl (5)

2.3. Initializing Bowl bowl3= new in Cupboard Bowl (3); At this point, the output is: Bowl (3). (Although BOWL3 is in front of Bowl4 and BOWL5, it is initialized later:

2.4. Initialize the cupboard constructor, output: cupboard, and execute the BOWL3.F1 (2) method, output: f1 ( 2)

So after the execution completes the static cupboard cupboard = new cupboard (), the output is:

Bowl (4) Bowl (5) Bowl (3) Cupboardf1 (2)

Step three: Perform the print of main function, output: Init3.main ()

Fourth step: Execute new Cupboard (); (This does not execute static member variables Bowl4 and BOWL5, because they have been initialized and do not need to be executed)

4.1. Initialize Bowl bowl3= new Bowl (3) in cupboard, at which point the output is: Bowl (3).

4.2. Initialize the cupboard constructor, output: cupboard, and execute the BOWL3.F1 (2) method, output:F1 (2)

Output Result:

Bowl (3) Cupboardf1 (2)

Fifth step: Perform the print of main function, output: Init3.main ()

Sixth step: Execute new Cupboard (); (This does not execute static member variables Bowl4 and BOWL5, because they have been initialized and do not need to be executed)

6.1. Initialize Bowl bowl3= new Bowl (3) in cupboard, at which point the output is: Bowl (3).

6.2. Initialize the cupboard constructor, output: cupboard, and execute the BOWL3.F1 (2) method, output:F1 (2)

Output Result:

Bowl (3) Cupboardf1 (2)

Seventh step: Execute table.f2 (1); output:F2 (1)

Eighth step: Execute cupboard.f3 (1); output: F3 (1)

Subsection: The initialization order is first a static object (provided that the object is not initialized) and then a non-static object.

(3) Static block initialization

Using static{...} in a class represents a static block that will initialize the data before the class is used.

Static blocks, like static variables, are initialized only once. Also, the order of execution is performed from top to bottom, where static blocks are the same as static methods.

Trigger conditions:

1. First generation of objects of this class;

2. First-time access to static members belonging to this class.

classstaticblock{ Public StaticString s = "Staticblock"; Static{System.out.println ("Staticblock.enclosing_method ()"); }}classinvokestaticblock{ Public Static voidMain (string[] args) {NewStaticblock ();//generate a reference to an objectString ss = Staticblock.s;//use static members of this object    }}

Summarize:

Object creation Process:

1. When creating an object instance, the constructor is executed (the constructor can be considered a static method), and the Java interpreter must find the classpath to navigate to the class's. class file;

2. Load the class file and initialize all of the static data. Therefore, static initialization is performed only once when the class object is first loaded, and the initialization operation is no longer performed after loading again;

3. When an object is created, sufficient storage space is allocated on the heap;

4. This storage space is zeroed, the base type data is appended with the initial value, and the reference type is null;

5. Perform all initialization actions that occur with the field definition, that is, if the field has an initial value, the default initial value is modified, and the new value is assigned;

6. Execute the constructor.

Java Learning Note II: Initialization (II)

Related Article

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.