Java in the constructor (Constructor)

Source: Internet
Author: User

Most of the content goes from:Http://tech.it168.com/j/2006-05-18/200605181021879.shtml       

A constructor is a special method that is called automatically when an object is created in order to initialize it. The name of the constructor should match the name of the class.

When objects are created, the system initializes the properties of the object by default, the value of the base type property is 0 (numeric type), False (Boolean type), and all reference types are set to NULL.

The constructor can change this default initialization.

The role of the constructor: is an important way to create Java objects, is it said that the constructor is entirely responsible for creating Java objects?

A: It is an important way to create Java objects, and when the constructor is called with the New keyword, the constructor does return objects of that class, but this object is not created entirely by the constructor.

The first thing to note is that the Java constructor is not a function, so he cannot be inherited, which is common when we write the constructor of a subclass when we extends, even if the subclass constructor parameter is exactly the same as the parent class, we write super for this reason.

 The constructor's modifier is limited, only the public private protected the three, other such as any modifier can not be used, that is, the constructor is not allowed to be famous as abstract, synchronization, static and other access restrictions outside the form.     because the constructor is not a function, it does not have a return value and does not allow a return value.    But here to illustrate, the constructor allows a return statement, but return does not return anything, if you specify the return value, although the compiler will not report any errors, but the JVM will think he is a constructor with the same name, This will cause some inexplicable errors that cannot be found in the constructor, which is to be doubly noted.  Some unexpected problems often arise when we extends a subclass, and here I say something about the constructor.   

Let's start by talking about the order in which Java is constructed (not the process of loading classes).

  the rough process of construction is as follows:     1, allocating object space, and initializing the members of the object to 0 or null, Java does not allow the user to manipulate an indeterminate value object.     2, the implementation of the explicit initialization of the property value (here is a little change, one will explain, but in general).     3. Execute the constructor     4. Associating a variable to an object in the heap   

Introduce the preparation of the knowledge, in order to explain the process in detail in a moment.

 

  This (), super () is if you want to call other constructors or control the parent class constructor with the data passed into the parameter in the current constructor or 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 row of the constructor, in the subclass if you want to invoke the parent class's constructor to initialize the part of the parent class, call Super () with the appropriate arguments, if you call the constructor of the parent class with a super () with no arguments (and you do not use this () To invoke other constructors), the default constructor of the parent class is called, and if the parent does not have a default constructor, the compiler will report an error.

Note here that we often inherit the parent class when the constructor does not write about the content of the parent class, and if the parent class does not have a default constructor, the compiler adds a default constructor that gives you trouble.

[Java]View PlainCopyPrint?
  1. Public class Manager extends Employee {
  2. Private  String Department;
  3. Public Manager (string name, double Salary, String dept)
  4. {
  5. Super  (name, salary);
  6. Department = Dept;
  7. }
  8. Public Manager (string n, String dept) {
  9. Super  (name);
  10. Department = Dept;
  11. }
  12. Public Manager (String dept) {
  13. //There is no super (), the compiler automatically adds an empty parameter to the default super constructor, which causes a compilation error if there is no default constructor for empty parameters in the employee class   
  14. department = D;
  15. }
  16. }

Java in the constructor (Constructor)

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.