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.
Use the constructor to ensure Initialization
It can be assumed that the initialize method is added for each written class.
The method name reminds you to call initialize () before using its object ().
However, this colleague means that the user must remember to call this method by himself.
In Java, by providing constructors, the class designer can ensure that every object can be initialized.
When creating an object, if it has a constructor, Java will automatically call the corresponding constructor before the user has the ability to operate the object to ensure initialization.
The constructor uses the same name as the class.
The constructor that does not receive any parameters is called the parameter-free constructor or the default constructor.
In Java, "initialization" and "creation" are bundled together and cannot be separated.
Method overload
1. Identify overload Methods
Each overload method must have a unique list of parameter types. You can even use different parameter sequences (but generally, do not do this, which makes the code difficult to maintain ).
2. Heavy Loads involving basic types
The base class type can be automatically upgraded from a "smaller" type to a "larger" type. This process involves heavy loads and may be confused.
If the input data type (actual parameter type) is smaller than the formal parameter type declared in the method, the actual data type will be upgraded.
Char is slightly different. If the method that just receives the char parameter cannot be found, the char will be upgraded directly to int.
If the input parameter is large, type conversion must be used to perform narrow conversion. Otherwise, the compiler reports an error.
3. Use the return value to distinguish the overload method.
Sometimes, you don't care about the return value of the method, so it doesn't work.
Default constructor
If no constructor exists in the class you write, the compiler will help you create a default constructor.
However, if you have defined a constructor, the compiler will not create a default constructor for you.
This keyword
"Reference of the operated object" is the first parameter.
Suppose you want to get the reference of the current object within the method.
Because this reference is passed by the compiler "secretly", no identifier is available.
However, there is a special keyword "this ".
This can only be referenced within a method, indicating a reference to the object that calls the method.
If you call another method of the same class within the method, you do not need to use this to call it directly (of course, you can also add ).
Generally, an input parameter with the same name and a domain variable have the same name. You can use this to indicate that it is my own, not the input parameter.
1. Call the constructor In the constructor
Package CN. partner4java. constructor. demo1;/*** 1. You can use this (...) to call the constructor In the constructor (...) * 2. The called constructor method must be placed in the first line of the called constructor * 3. Because 2, only one constructor of this class can be called in one constructor * @ author partner4java **/public class flower {int petalcount = 0; string S = "Initial Value "; public flower (INT petalcount) {This. petalcount = petalcount; system. out. println ("constructor int Arg only, petalcount =" + petalcount);} public flower (string s) {system. out. println ("constructor string Arg only, S =" + S); this. S = s;} public flower (INT petalcount, string s) {This (petalcount); // This (s); // can't call two; (constructor call must be the first statement in a constructor) This. S = s; system. out. println ("string & int s ");}}
2. Meaning of static
The static method is the method without this.
A non-static method cannot be called within a static method, which is the opposite.
Without creating an object, the static method is called only through the class itself. This is actually the main purpose of the static method.
Similar to the global method, the global method is not allowed in Java.
Cleanup: final processing and garbage collection
JVM:
Http://blog.csdn.net/partner4java/article/details/7206650
Http://blog.csdn.net/partner4java/article/details/7207741
End method:
Http://blog.csdn.net/partner4java/article/details/7061188
Member Initialization
Local variables cannot be initialized.
package cn.partner4java.init.demo2;public class Init {private void f() {int i;i++;//Error,The local variable i may not have bean initialized}}
The basic type of global variables is initialized by default.
1. Specify Initialization
Note that the initialization and initialization sequence are specified.
Constructor Initialization
Automatic initialization cannot be prevented. It will occur before the constructor is called.
1. Initialization order
Within the class, the sequence of variable definitions determines the initialization sequence.
Even if variable definitions walk between method customization, they still get initialization before any method (including constructors) is called.
2. Static Data Initialization
No matter how many objects are created, static data occupies only one storage area.
The static keyword cannot be applied to local variables because it can only apply to fields.
If a static basic type field is not initialized, it will obtain the standard initial values of the basic type. If it is an object reference, the default initialization value is null.
When is the static storage region initialized?
Package CN. partner4java. init. demo3; public class bowl {public bowl (INT marker) {system. out. println ("bowl (" + marker + ")");} void F1 (INT marker) {system. out. println ("F1 (" + marker + ")") ;}} package CN. partner4java. init. demo3; public class table {static bowl bowl1 = new bowl (1); public table () {system. out. println ("table ()"); bowl2.f1 (1);} void F2 (INT marker) {system. out. println ("f2 (" + marker + ")");} static bowl Bowl2 = new bowl (2);} package CN. partner4java. init. demo3; public class cupboard {bowl bowl3 = new bowl (3); static bowl bowl4 = new bowl (4); Public cupboard () {system. out. println ("cupboard ()"); bowl4.f1 (2);} void F3 (INT marker) {system. out. println ("F3 (" + marker + ")");} static bowl bowl5 = new bowl (5);} package CN. partner4java. init. demo3;/*** to summarize the creation process, suppose there is a class named dog: * 1. The constructor is actually a static method even if no static keyword is explicitly used. * Therefore, when the recipient creates an object with a class of dog (the constructor can be regarded as a static method), * or when the dog's static method/static domain is accessed for the first time, the Java interpreter must search for the class path to locate the dog. class file. <Br/> * 2. Load dog. Class. All static initialization actions are executed. * Therefore, static Initialization is only performed once when the class object is loaded for the first time. <B/> * 3. When an object is created using new dog (), sufficient storage space is allocated to the dog on the heap. <Br/> * 4. This bucket is cleared, which automatically sets all the basic data types of dog from the default value and references to null. <Br/> * 5. Execute all initialization actions that appear in the field definition. <Br/> * 6. Execute the constructor. * @ Author partner4java **/public class staticinitialization {public static void main (string [] ARGs) {system. out. println ("creating new cupboard in main"); new cupboard (); system. out. println ("creating new cupboard in main"); new cupboard (); table. f2 (1); cupboard. f3 (1);} static table = new table (); static cupboard = new cupboard ();} // background print: // bowl (1) // bowl (2) // table () // F1 (1) // bowl (4) // bowl (5) // bowl (3) // cupboard () // F1 (2) // creating new cupboard in main // bowl (3) // cupboard () // F1 (2) // creating new cupboard in main // bowl (3) // cupboard () // F1 (2) // F2 (1) // F3 (1)
3. Explicit static Initialization
Class Cups {static Cup cup1;static Cup cup2;static{cup1 = new Cup(1);cup2 = new Cup(2);}}
4. Non-static instance Initialization
Package CN. partner4java. init. demo4; public class mug {public mug (INT marker) {system. out. println ("mug (" + marker + ")");} void F (INT marker) {system. out. println ("F (" + marker + ")") ;}} package CN. partner4java. init. demo4;/*** run the instance initialization clause * @ author partner4java **/public class mugs {mug mug1; mug mug2; {mug1 = new mug (1) before the constructor ); mug2 = new mug (2); system. out. println ("mug1 & mug2 initialized");} public mugs () {system. out. println ("mugs ()");} public mugs (int I) {system. out. println ("mugs (INT)");} public static void main (string [] ARGs) {system. out. println ("Inside main ()"); New mugs (); system. out. println ("New mugs () completed"); New mugs (1); system. out. println ("New mugs (1) completed") ;}/// background print: // inside main () // mug (1) // mug (2) // mug1 & mug2 initialized // mugs () // new mugs () completed // mug (1) // mug (2) // mug1 & mug2 initialized // mugs (INT) // new mugs (1) completed