The constructor initializes an object when it is new. The process of Initializing an object using new is as follows:
(1) First, execute staticCodeIf the class has a parent class, the static code block of the parent class is executed first.
(2) Then execute the constructor of the class. If the class has a parent class, execute the constructor of the parent class first. Thus, a constructor chain can be formed until the top-level parent class.
Run the following code:
Public class objtest {public static void main (string [] ARGs) {children OBJ = new children () ;}} class children extends parent {static {system. out. println ("Children static block... ");} Public Children () {system. out. println ("Children constructor block .... ");} public void test () {system. out. println ("instance block .... ") ;}} Class Parent {static {system. out. println ("parent static block... ");} public parent () {system. out. println ("parent constructor block... ");}}
Result:
Parent static block...
Children static block...
Parent constructor block ..
Children constructor block ....
From this we can understand the object initialization process, the most important of which is the constructor. The constructor is to initialize an object. Next, let's talk about the constructor method. When we do not specify a constructor for a class, compileProgramAn empty constructor is automatically generated. When we specify a non-empty constructor for a class, the compiler will not automatically generate a constructor.
For example:
Class OBJ {public static void main (string [] ARGs) {New OBJ ();}}
The new OBJ () function is to create an object and call the default constructor, even if we do not specify the constructor.
If the code is modified as follows:
Class OBJ {public static void main (string [] ARGs) {New OBJ ();} public OBJ (int ){}}
The compiler reports an error because a constructor has been specified. As if we have not set any builder,
Compile the program and say, "You seem to need a builder, so let's create one for you ." But if we write a builder, the compiler will say, "Ah, you have written a builder, so I know what you want to do. If you do not place a default one, it is because you intend to omit it."
Therefore, if we specify a non-null constructor for a class, we usually need to manually specify a non-null constructor to facilitate class inheritance.
Next let's talk about the this keyword, which represents the current object.
First, let's look at the following code:
Class OBJ {void F (int I) {} public void test () {obj a = new OBJ (), B = new OBJ ();. F (1); B. F (2 );}}
This is a common object call method. How is it implemented internally? As follows,
OBJ. F (A, 1); obj. F (B, 2); specifies a handle for a method. We can also use the this keyword to call this. F (1); but in fact there is no need, the compiler will automatically help us. However, for special classes, you need to explicitly use the handle of the current object and use this when returning the handle of the current object.
Class OBJ {int A = 0; void g () {test (). Test (). Test () ;}obj test () {A ++; return this ;}}
We can construct a method chain in this way.
If you write multiple constructors for a class, you often need to call another builder in one builder to avoid repeated code writing. This can be done with the this keyword.
Class OBJ {int A = 0; Public OBJ (int A) {This. A = A;} public OBJ (string s) {} public OBJ () {This (4); // This ("AA"); system. out. println (".... ");}}
You can use this to call another constructor In the constructor. The this statement must be the first statement and cannot use two this statements. In the first constructor, the member variables and local variables are renamed. You can also use the this keyword to call member variables.
During object initialization, we also mentioned the static keyword, the member variable modified by the static keyword, and the method belongs to the class method and is global. all instances of this class share the data, you can use this data without creating an object. Instance variables. The instance method (without static modification) belongs to an instance. You must create an instance before using the data. therefore, you cannot call an instance method in a static method, because the instance method needs to determine the object. It should be noted that static code blocks. Static code blocks are executed when class bytecode is loaded. You can perform global initialization. The constructor performs initialization for a specific instance.