11-Play from zero javaweb-constructor

Source: Internet
Author: User

Supporting Video:

1. What is a constructor

2. Builder features

3. Custom constructors

4. Constructor overloading

First, what is a constructor?

  Let's look at the following line of code

New is not like calling a method after

We call the method that is called automatically when the object is created, called the constructor

The default constructor cannot be found in the class, and the default constructor can be seen by the anti-compilation tool

As you can see, there's a way to do this after you've translated it into a byte code.

User () {

}

This is the default constructor.

When the compiler compiles the source file, a default constructor is created

If we set a default value for a field setting

The default value is actually set in the default constructor.

Second, the role of the constructor

1. Create objects that must be used with new

2. Completion of initialization of objects

  

As you can see, the default values that we set are set in the default constructor method.

Three, the characteristics of the constructor

 The name of the ① constructor is the same as the name of the class that is currently located.
② prohibit defining return types, never use void as the return type.
③ in the constructor, you do not need to use the return statement.

If we do not write the constructor, we will create a constructor with no arguments by default.


 ① conforms to the characteristics of the constructor
② No parameters
③ if the class is using public, the default constructor is also public

Iv. Custom Constructors

We can do it ourselves. Define constructors, and custom constructors conform to the constructor's characteristics

1. The constructor that we write ourselves is called a custom constructor
2. If we define the constructor ourselves, the compiler no longer creates the default constructor
3. A class has at least one constructor.
4. Creating an object is actually calling the constructor.

  

   

V. Overloading of constructors

Overloading of constructors: As with overloads of methods
You can define multiple constructors, like the constructor's method name
Parameters are different, when you create an object, different constructors are called depending on the parameters

 

As you can see, a parameter is executed and a constructor is found,

When you create a constructor that uses two arguments, execute the constructor that will find two parameters.

Follow the number " code ant cockroach "

The first time to get the "zero Play to javaweb+ project" The latest update video and

Class "Code + software +xmind notes" in "code Ant Java Learning Exchange Group: 625973026" file sharing

netease Cloud: Click Watch the suggested speed watch online

11-Play from zero javaweb-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.