Constructor, java Constructor

Source: Internet
Author: User

Constructor, java Constructor
The Reference Type constructor is a special method for initializing a type instance to a good state. When creating a reference type instance, first allocate memory for the instance's data field, then initialize the additional fields of the object (type object pointer and synchronized block index), and call the instance constructor of the type to set the initialization status of the object. When constructing an object of the reference type, the memory allocated to the object is always first 0 before the instance constructor of the electrophoresis type, all fields not overwritten by the constructor are guaranteed to get 0 or null values. Unlike other methods, the strength constructor can never be inherited. That is to say, only the class defined by itself can still be smoothly constructor. Because the instance constructor can never be inherited, the instance constructor cannot use the following modifiers: Virtual, new, override, sealed, and abstract. If the class does not display and define Any constructor, the C # compiler defaults to a default non-argument constructor. In her implementation, it simply calls the non-argument constructor of the base class. If the class modifier is abstract, the default constructor generated by the compiler is product; otherwise, the constructor is assigned a public accessible attribute. If the base class does not provide a non-argument constructor, the derived class must call a base class constructor. Otherwise, the compiler reports an error. If the class modifier is static (sealed and abstract), the compiler will not generate the default constructor in the class definition at all. One type can define multiple instance constructors. Each constructor must have different signatures, and each constructor can have different accessible attributes. To make the code "verifiable", the class strength constructor must call the constructor of the base class before accessing any fields inherited from the base class. If the constructor of the derived class does not show that a base class constructor is called, the C # compiler automatically generates a call to the default base class constructor. Finally, the System. Object public parameter-free constructor will be called. If the constructor does nothing, it will return directly. Because System. Object does not have an instance data field, its constructor has nothing to do. In rare cases, you can create a type instance without calling the instance constructor. A typical example is the MemberwiseClone method of the Object. This method is used to allocate memory, initialize additional fields of the object, and then copy the data of the source object to the new object. In addition, the constructor does not need to be called during the deserialization of objects using the runtime seriallizer. For deserialization, The GetUninitalizedObject or GetSafeUninitailizedObject method of the System. Runtime. Serialization. FormatterServices type is used to allocate memory to the object. No constructor is called during this period. Tip: do not call the virtual method in the constructor. The reason is that if the instantiated type overwrites the virtual method, the implementation of the derived type to the virtual method will be executed, but at this time, initialization of all fields in the hierarchy has not been completed (the instantiated Type constructor has not yet run ). Therefore, calling a virtual method will lead to unpredictable behavior. In the final analysis, this is because the actual type of the method to be executed is not selected until the virtual method is called. A value type (struct) constructor works differently from a reference type (class) constructor. CLR always allows creation of value-type instances, and there is no way to prevent value-type instantiation. Therefore, the constructor does not need to be defined for the value type. The C # compiler does not set the default parameter constructor for the value type inline. Let's take a look at the following code: internal struct Point {public int m_x, m_y;} internal sealed class Reactangel {public Point m_TopLeft, m_bottomRight;} the new operator must be used to construct a Rectangle, the constructor must be specified. In this example, the default constructor automatically generated by the C # compiler is called. Allocate memory for Reatangle. The memory contains two instances of the Point value type. Considering the performance, CLR does not take the initiative to call the constructor for each value type field included in the reference type. However, fields of the value type will be initialized to 0 or null as described above. CLR does allow a value type definition constructor, but it must be displayed for invocation. Internal struct Point {public int m_x, m_y; public Point (int x, int y) {m_x = x; m_y = y ;}} internal sealed class Reactangel {public Point m_TopLeft, m_bottomRight; public Reactangel () {this. m_TopLeft = new Point (1, 2); this. m_bottomRight = new Point (100,200);} The Value Type instance constructor can be executed only when the display call is performed. Therefore, if the Rectangle constructor does not use the new operator to call the Point constructor, The m_TopLeft and m_bottomRight fields of the Reatangle will be initialized, the m_x and m_y fields in the two point fields will be 0. rewrite the above Code: internal struct Point {public int m_x, m_y; public Point () {m_x = 5; m_y = 6 ;}} internal sealed class Reactangel {public Point m_TopLeft, m_bottomRight; public Reactangel () {}} now, when constructing a new Rectangle class, how much will the m_x and m_y fields in the two Point fields be initialized? Is it 0 or 5? You may think that the C # compiler will generate code in the Reactangel constructor and automatically call the default no-argument constructor of Point for the two fields of Reactangel. However, to enhance the runtime performance of the application, the C # compiler will not automatically generate such code. In fact, even if the value type provides a parameter-free constructor, many compilers will never generate code to call it. To execute the value type-free constructor, developers must add code that displays the call Value Type constructor. But will the two fields of Point be initialized to 0 for this reason? The result is that the C # compiler intentionally does not allow a value type definition non-argument constructor to prevent developers from confusion about when such constructor is called. Since no parameter-free constructor can be defined, the compiler will never generate code that automatically calls it. Without a parameter constructor, value-type fields are always initialized to 0 or null. Type constructor: it is also called a static constructor, a class constructor, or a type initializer. The Type constructor can apply and reference types and value types. The instance constructor is used to set the initial status of an instance. Correspondingly, the Type constructor is used to set the initial state of the type. By default, no Type constructor is defined for the type. If the type is defined, only one constructor can be defined. In addition, the Type constructor never has parameters. Internal sealed class SomeRefType {static SomeRefType () {// execute the code here for the first visit} internal struct SomeValType {static SomeValType () {// At the first visit, run the code here.} You can see that the definition Type constructor is similar to the definition no-argument instance constructor. The difference is that it must be marked as static. In addition, the Type constructor is always private. The reason for being private is to prevent any developer from writing code to call it. The CLR is always responsible for its calls. Tip: although the Type constructor can be defined in the value type, do not do this because CLR sometimes does not call the static constructor of the Value Type: for example, internal struct SomeValType {static SomeValType () {Console. writeLine ("this sentence will never be displayed");} public int m_x;} class Program {static void Main (string [] args) {SomeValType [] a = new SomeValType [10]; a [0]. m_x = 123; Console. writeLine (a [0]. m_x); Console. the code of the ReadKey () ;}} Type constructor can only access static fields of the type, and its unconventional purpose is to initialize these fields.

Related Article

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.