Java Programming thought Note (ii)--initialization

Source: Internet
Author: User

I. Issues

With the development of the computer revolution, "unsafe" programming has become one of the main reasons for the high cost of programming.

  initialization and cleanup are just two issues that involve security.

Two. Java Solutions

Constructor: A special method. In Java, "Initialize" and "create" are bundled together, and they cannot be separated.

Garbage collector: Frees useless objects.

Three. Method overloading

Why do you want to talk about method overloading here? I think first: the constructor is a special method, and again, the constructor has a parameter constructor in addition to the default constructor. A method overload must be used for constructors that have the same method name and have different form parameters.

Decision conditions for Differentiating method overloads

1. Whether the number of parameters is the same

2. Whether the parameters are of the same type

As long as one of them is no, it is the method overload, and the prerequisite for overloading must be remembered: The method name must be the same.

Four. Constructor calls in constructors

1. Although you can call a constructor with this, you cannot call two. In addition, you must place the constructor call at the very beginning, or compile the error.

2. In addition to the constructor, the compiler prohibits invoking the constructor in any other method.

Five. Member initialization path

1. All variables are properly initialized before they are used, the base type of the class defaults to the default value of the base type, the local variable must be specified by the programmer, and in the class an object reference is not initialized, and a special null value is obtained.

2. Programmer specifies variable initialization value

3. Initializes the variable value through the constructor, but the automatic initialization is done before the constructor is initialized.

Six. Sequence of variable initialization

The order in which the variables are defined determines the order in which they are initialized, even if the variable definition walks between the method definitions.

Seven. static data initialization

No matter how many objects are created, static data occupies only one copy of the storage area, and the static keyword cannot be used for local variables, so it can only be used for domains.

Eight. Non-static instance initialization

The code is as follows:

Class mug{Mug (intmaker) {Print ("Mug (" +maker+ ")"); }                  voidFintmaker) {Print ("F (" +maker+ ")"); }} PublicClass mugs{Mug mug1;   Mug Mug2; {MUG1=NewMug (1); Mug2=NewMug (2); Print ("Mug1&mug2 initial"); } mugs () {print ("Mugs (int)"); }    Public Static voidMain (string[] args) {print ("Inside Main ()"); Newmugs (); Print ("New Mugs () completed"); NewMugs (1); Print ("New Mugs (1) completed"); }}                  

As you can see from the output, the instance initialization clauses are executed before the two constructors.

Nine, a summary of 678

 1. Initialize the static variable first (initialize when the. class file is first accessed, initialize it only once), and then initialize the non-static variable

2. Non-static variables are copied to the default value when the object is created.

3. Execute the programmed value as the initial value specified by the variable.

4. Execute the constructor.

Java Programming thought Note (ii)--initialization

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.