Java Fundamentals Review-10 (construction method)

Source: Internet
Author: User

1. What is the format of the construction method? What are the precautions?

A: Modifier constructor method Name (parameter list)

{

}

Note: (1) The construction method does not return a value type and does not require a write return value. Because it is for building objects, the object is created, and the method executes the end.

(2) The construction method name must be consistent with the class name.

2. Can the construction method be overloaded? Can I have a return statement inside?

A: The construction method can be overloaded, there can be no return statement.

3, there are several ways to assign a member variable? What's the difference?

Answer: three different ways.

The first approach is to assign values directly when declaring member variables;

The second way is to use the Get,set method to assign a value by means of an argument;

The third way is to assign a value when creating a variable by means of a constructor function.

4. If no construction method is specified, the compiler adds an empty construction method to the class file by default at compile time.

5, can only call in the construction method of other construction methods, through this (pass the parameter) way to call. This (pass parameter) should be put in the first place, otherwise it will error.

6, the child's construction method, the default will have a super () method, the purpose is to inherit the parent class's construction method into the subclass. This super () default top, super (pass parameter) and this (pass parameter) will be in the top side conflict, should try to avoid this situation.

Java Fundamentals Review-10 (construction method)

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.