Java programming ideology note (2) -- initialization and java programming ideology
I. Problems
With the development of the computer revolution, the "insecure" programming method has gradually become one of the main reasons for the high cost of programming.
Initialization and cleanup are two security issues.
Ii. java Solution
Constructor: a special method. In java, "initialization" and "creation" are bundled together. The two cannot be separated.
Garbage Collector: releases useless objects.
3. Method Overloading
Why is there a method overload here? First of all, I think the constructor is a special method. In addition to the default constructor, the constructor also has a parameter constructor. Method Overloading is required to ensure that constructors with the same method name and different form parameters exist simultaneously.
Determining Conditions for distinguishing method Overloading
1. Whether the number of parameters is the same
2. Whether the parameter types are the same
If one of them is no, the method is overloaded. You must remember the premise that the method name must be the same.
4. Call the constructor In the constructor
1. Although you can use this to call a constructor, you cannot call two constructor. In addition, the constructor call must be placed at the very beginning; otherwise, the compilation is incorrect.
2. Except the constructor, the compiler prohibits the calling of constructor in any other method.
5. Member initialization path
1. all variables can be properly initialized before use. The basic type of the class defaults to the default value of the basic type. The local variable must be specified by the programmer. in the class, if an object reference is not initialized, a special null value is obtained.
2. the programmer specifies the variable initialization value.
3. initialize the variable value through the constructor, but the automatic Initialization is completed before the constructor initialization.
6. Variable initialization sequence
The sequence of variable definitions determines the initialization sequence, even if the variable definitions walk between method definitions.
VII. Static Data Initialization
No matter how many objects are created, static data occupies only one storage area. The static keyword cannot be used for local variables, so it can only be used for fields.
8. Non-static instance Initialization
The Code is as follows:
Class Mug{ Mug(int maker){ print("Mug("+maker+")"); } void f(int maker){ print("f("+maker+")"); }}public Class Mugs{ Mug mug1; Mug mug2; { mug1= new Mug(1); mug2= new Mug(2); print("mug1&mug2 initial"); } Mugs() { print("Mugs(int)"); } public static void main(String[] args) { print("Inside main()"); new Mugs(); print("new Mugs() completed"); new Mugs(1); print("new Mugs(1) completed"); }}
The output shows that the instance initialization clause is executed before the two constructors.
IX. Summary
1. initialize static variables (initialize only once when accessing the. class file for the first time), and then initialize non-static variables
2. Non-static variables will be copied to the default value when an object is created.
3. The execution programming value is the initial value specified by the variable.
4. Execute the constructor.