1. Constructor initialization
The new object first assigns a piece of memory to the device and calls the constructor to initialize it.
Construction method: The construction method is consistent with the type, and there is no return type. Each class has a default construction method.
The constructor method can be overloaded with a normal method to pass in different parameters. Distinguishes the construction method, only by the number of parameters, different types to distinguish.
public class Contruct {
contruct () { //parameterless construct method
System.out.println ("constructor");
}
contruct (int i) { //Parametric constructor method
System.out.println ("constructor:" +i);
}
Contruct (double s) {
System.out.println ("constructor:" +s);
}
public void contruct (int i) { //Normal method (unstructured method)
System.out.println ("non-constructed Method");
}
2. Member initialization
class, the base type is initialized by default, and the object needs to be initialized manually to avoid errors when used.
Local member variables need to be initialized, including basic types, or compile an error.
Non-static variables and methods cannot be directly referenced within a static method.
public class Testmain {
int J;
The static int k;//class's member variable, which is initialized to 0 public
static void Main (string[] args) {
testmain t;
for (int i=0; i<j; i++) { //cannot make a static reference to the Non-static field J
t.f (); The local variable t could not have been initialized
new contruct ();
New Contruct (i);
System.out.println ("K:" +k); k:0
}
} public
void F () {
int m;//local variable
System.out.println (j); J non-local variable with the default initialization value of 0
System.out.println (m);//the local variable m could not have been initialized
}
}
3, array initialization
Method of array initialization:
1, static initialization: int[] a1 = {1,2,3,4,5};
2, dynamic initialization: First declare int[] A2; for () {a2[i]};
3, the creation of array objects, default initialization int[] a3 = new int[];
public class Arraytest {public
static void Main (string[] args) {
int[] a1 = {1,2,3,4,5}; (1) Static initialization of
int[] A2; Declaration
int[] a3 = new int[5];//(2) creates an array object, which is initialized to 0
a2 = a1;//referenced assignment for
(int i=0; i<a2.length; i++) {
a2[i] ++; (3) dynamic initialization
} for
(int j=0; j<a1.length; j) {
prin ("a1[+j+"]: "+a1[j");
for (int k=0; k<a3.length; k++) {
prin ("a3[" +k+ "]:" +a3[k]);
}
static void Prin (String s) {
System.out.println (s);
}
}
3, clear
GC:JVM garbage collection mechanism, recycling new out of memory, only for memory recycling, when the object is no longer used when automatically invoked.
The difference between final, finally, finalize
Final: Define properties, methods, and classes. The defined property cannot be changed; The defined method is not overridable; The defined class is not inheritable.
Finally: part of the exception handling statement that always executes try{}catch{}finally{}
Finalize: This method of invoking the reclaimed object when garbage collection is used for garbage collection.