Java Builder (definition, function, principle) __java

Source: Internet
Author: User
Tags class manager modifiers
The first thing to note is that the Java constructor is not a function, so he cannot be inherited, which is more common when we extends the constructor of a subclass, even though the subclass constructor parameters are exactly the same as the parent class, which is why we write super.
Constructor modifiers are limited, only public private protected these three, others such as any modifiers cannot be used, which means that constructors are not allowed to be famous as abstractions, synchronizations, Static, and other forms of access restrictions.
Because the constructor is not a function, it does not return a value, nor does it allow a return value. But here's to say that the constructor allows a return statement, but returns nothing, and if you specify a return value, the compiler does not report any errors, but the JVM thinks that he is a function with the same name as the constructor. This will cause some inexplicable errors that cannot find the constructor, and here is a double note.


When we extends a subclass, there are always some unexpected problems, and I'm here to say something about the constructor.
First, let's say the order of Java when constructing an instance (without discussing the process of loading classes)
The rough process of construction is as follows
1. Allocate object space, initialize the space memory into binary 0, and initialize the member of the object to 0 or null, Java does not allow the user to manipulate an indefinite value object.
2. Perform an explicit initialization of a property value (here's a little bit of a change that will explain, but roughly)
3. Execute constructor
4. Associating a variable with an object in the heap


Introduction to the preparation of knowledge, in case a later to elaborate the process of this
This () super () is used when you want to call another constructor or control the parent class constructor by passing in the parameters in the current constructor or the data in the constructor. In a constructor you can only use one of this () or super (), and the location of the call can only be in the first line of the constructor , in subclasses, if you want to invoke the constructor of the parent class to initialize the part of the parent class, then call Super () with the appropriate arguments, and if you invoke the constructor of the parent class with a super () with no parameters (and you do not use this () to invoke the other constructor), the default constructor for the parent class is invoked. If the parent class does not have a default constructor, the compiler will report an error, note here, we often inherit the parent class when the constructor does not write the contents of the parent class, at this point if the parent class does not have a default constructor, the compiler added default constructor will give you the problem of trouble oh. For example: Class B extends A{public B () {}} does not have any information about the parent class constructor, when the default constructor for the parent class is invoked.


For example in a SL-275
1 public class Manager extends Employee {
2 Private String Department;
3
4 public Manager (string name, double salary, String dept)
{
5 Super (name, salary);
6 Department = Dept;
7}
8 public Manager (string n, String dept) {
9 super (name);
Ten department = Dept;
11}
Public Manager (String dept) {//There is no super (), the compiler automatically adds a default super constructor for an empty parameter, and if there is no default constructor for null arguments in the employee class, it can cause a compilation error
department = D;
14}
15}


You must place the super or this constructor in the first line of the constructor, or else the compiler will automatically place a super constructor of an empty argument, and other constructors can call super or this and call it a recursive construct chain. The final result is that the constructor of the parent class (which may have a multilevel parent constructor) is always executed before the constructor of the subclass, and the recursive invocation of the parent class builder


In the process of constructing the class instance concretely, the second and third steps of the top process are somewhat different, in this order, when the object space is assigned and the object member is initialized to the default value, and the constructor is called recursively from the root of the inheritance tree, and the execution process of each constructor is this:
1. Parameters of the Bind constructor
2. If you call this explicitly, call the this constructor recursively and then skip to step 5
3, recursive call explicit or implicit parent class constructor, except object, because it does not have a parent class
4, the execution of an explicit instance variable initialization (that is, the second step in the top process, the call returned after the execution, this step is equivalent to the implementation of the parent constructor implicitly after execution, looks like a special treatment)
5, the execution of the other parts of the constructor


The steps here are very important.


It is clear from this step that the recursive call process when this instance is initialized is an estimate that you should be able to understand how this recursive construct chain works.


Here's an example of SL-275 to give you a full understanding of this recursive process.


public class Object {
...
Public Object () {}
...
}
public class Employee extends Object {
private String name;
Private double salary = 15000.00;
Private Date birthdate;
Public Employee (String N, Date DoB) {
Implicit super ();
name = N;
Birthdate = DoB;
}
Public Employee (String N) {
This (n, null);
}
}


public class Manager extends Employee {
Private String Department;
Public Manager (string n, string d) {
Super (n);
department = D;
}
}


When creating Manager ("Joe Smith", "Sales"): When, the steps are as follows
0 Basic Initialization
0.1 allocate memory for the complete Manager object
0.2 initialize all instance variables to their default values (0 or null)
1 Call Constructor:manager ("Joe Smith", "Sales")
1.1 Bind constructor parameters:n= "Joe Smith", d= "Sales"
1.2 No explicit this () call
1.3 Call Super (n) for Employee (String)
1.3.1 Bind constructor parameters:n= "Joe Smith"
1.3.2 Call this (n, null) for Employee (String, Date)
1.3.2.1 Bind constructor parameters:n= "Joe Smith", Dob=null
1.3.2.2 no explicit this () call
1.3.2.3 Call Super () for Object ()
1.3.2.3.1 No binding necessary
1.3.2.3.2 no This () call
1.3.2.3.3 no Super () call (Object is the root)
1.3.2.3.4 no explicit variable initialization for Object
1.3.2.3.5 No.
1.3.2.4 Initialize explicit Employee variables:salary=15000.00 Note: The subclass does not initialize the value of the instance variable until the parent constructor returns.
1.3.2.5 Execute body:name= "Joe Smith"; Date=null;
1.3.3-1.3.4 steps skipped
1.3.5 Execute Body:no body in Employee (String)
1.4 No explicit initializers for Manager
1.5 Execute body:department= "Sales"


This process explains everything, and this step is to be noted. There are still some things to be covered here.




Some of the considerations that are written in the following are used in the constructor.


One, the constructor must not create its own instance, otherwise it will cause the call stack overflow error. This rule also applies to an object's instance variable, which must not be initialized in the definition or constructor if the object has its own reference.


Class A
{
A _a = new A ();


Public A ()
{
_a = new A ();
A _b = new A ();
}
}


Above three kinds of situation will cause stack overflow, hehe, this will cause an infinite recursive call stack.


If the parent class is an abstract class, you can initialize it by calling the constructor of the parent class, and initialize the data in it.
Third, if you want to call a method in the constructor, declare the method private.
This rule requires some explanation, and if you are calling a Non-static method in your parent class constructor, and this method is not private and is overloaded by a quilt class, then recursively call the constructor of the parent class in the process of actually creating the subclass. The parent constructor's invocation of this method will actually invoke the subclass's method due to polymorphism. When this subclass method needs to use an instance variable in a subclass, an exception occurs because the variable is not initialized (as to why the instance variable in the subclass has not been initialized to refer to the instance initialization process above), This is a situation that Java does not want to see. When a method called in a parent class constructor is a private method, polymorphism does not appear, and the parent class constructor invokes the subclass method, which guarantees that the parent class always calls its own method. Even if an instance variable in the parent class is called in this method, the variable is not initialized (variable initialization is always done before the current class constructor body executes).

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.