Java class constructor

Source: Internet
Author: User

When using new to create an object, Java calls its constructor. The constructor and method have no return value, and their names are consistent with the class name. In the constructor, we can initialize some fields of the class. In this way, after an object is created, these objects have the default initial values.

When our class inherits the parent class, when creating a subclass object, it first calls the constructor of the parent class. In fact, before calling the constructor of the parent class, the constructor of the Object class will continue to be called. After the constructor of the Object class is called, the constructor of the parent class will be called, and the constructor of the subclass will be called in turn. The reason for this is that the fields value of our subclass object may depend on the parent class, so the parent class must initialize its fields. Generally, when writing a subclass constructor, you do not need to write

Super ()

JVM automatically calls the default constructor of the parent class. However, when we do not want to call the default constructor of the parent class, we want to call the constructor of the parent class with parameters, at this point, we need to explicitly call super (arg1, arg2 ,...), it should be noted that its call must be the first line in the constructor. The reason is also very simple, that is, to prevent the sub-class constructor from being called first.

We can also call another overload constructor method of the subclass in the subclass constructor.

This (arg1, arg2 ,...);


Note: this here is different from this in the method. this in the method refers to the current object, and this here refers to pointing to the same class, another constructor for different parameter lists.

When we reload our constructor, the JVM will not automatically create a default non-argument constructor as before. At this point, we should add it ourselves.

When we write the following code,

Dog d = new Dog ();

In fact, before executing the constructor, the system first allocates memory space for the object and executes Default initialization for the object. At this time, the object has been generated, this object cannot be accessed by external programs. It can only be referenced by this constructor. When the constructor's execution body ends, this object is returned as the constructor's return value. It is usually assigned to another variable of the reference type, so that external programs can access this object.



Next I will talk about the class constructor concept in java. As we all know, java, as an OO language, has three key points like C ++ and Smalltalk: 1. ADT, 2. Continue, 3. Polymorphism

Of course, for java, the concept of classes is the top priority. Constructors are required for class construction and object implementation.

For example:

Class PRofesser () {private string name; private int number; public Professer (string n, int no) {name = n; number = no;} public teach (){}}


Next we will instantiate the object of this class: Professer p = new Professer ()
The above is the constructor syntax. The key words are used here.

New

There is nothing to worry about here, so we will continue to explain the constructor problem with some examples:

Hypothesis:

Class OldProfesser () {private string name; private int number; private int age; public do (){}}


Someone may ask why there is no definition constructor in this OldProfesser class? In fact, java has defined a default constructor without parameters, that is, OldProfesser (){}
OK, so far, there is no problem. However, once OldProfesser continues Professer and is instantiated, the compiler will report an error. LOOK:

Class OldProfesser extends Professer () {private string name; private int number; private int age; public do () {} public static void mian (string [] args) {OldProfesser o = new OldProfesser ();}


Sorry, you got an error message. Someone asked, you said that the OldProfesser constructor is the default one, the OldProfesser does have such a non-argument constructor. In this way, it must be correct if the OldProfesser is not a subclass of Professer.

All you need to know is the following rule:
1. If a class does not set a word definition constructor, the compiler defaults to a non-argument constructor.
2. When a subclass calls the constructor, it always calls the constructor of the base class first.
3. If the constructor is defined, the default constructor does not exist.

In the preceding example, before OldProfesser calls the default constructor, the compiler automatically calls the default constructor of the base class Professer. However, the Professer has a custom constructor, so the default constructor does not exist, compiler error ............
So what should we do? As shown in the following figure.

Class OldProfesser extends Professer () {private string name; private int number; private int age; Oldprofesser (string n, int no, int age) {super (n, no); this. age = age ;}public do (){}}


This is to explicitly call the base class constructor in the subclass. Note that super must be the first execution statement in. In fact, there is another way to add a default constructor that disappears in the Professer class, as shown below:

Class Professer () {private string name; private int number; public Professer (string n, int no) {name = n; number = no;} parse se () {} public teach (){}}


It is worth mentioning that in the constructor method, the constructor name must be exactly the same as the class name, including case.

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.