Java constructor (constructor method)

Source: Internet
Author: User
Tags modifier

Constructor (constructor, construction method) 3W what? Why How? What is it, why, how to use it? Precautions?

What? What: a constructor (also called a constructor method) is a special method in itself (but it has nothing to do with the method, which is used to call the constructor, lest it be confused with the method), which is called only when the new object is instantiated.
Why? Why use it: Why use a constructor, if you want to instantiate a new object, you need to use the keyword new to complete, but in addition to the new keyword, it is possible to do some initialization for the object instantiation, this time need to construct the method of support. So using a constructor is what it does to make it work.
The constructor function is: 1. To create an object of a class
2. Initialize the properties of an object
Characteristics of the constructor
1. It has the same name as the class

2. It does not declare a return value type.

3. Can not be modified by static, final, synchronized, abstract, native, cannot return the value of the statement
How? How to use: when it comes to how to use it must have a certain format, can not be used, to have a definition of the format
Constructor declaration format: Permission modifier class name (parameter list, ...). {} (Note that the constructor does not return a value type) The
method declares the format permission modifier to return the value of the data type method name (parameter list, ...). {
Program Statement
[return constant or variable]
}
constructor differs from method: 1 declaration defines a format in which the constructor does not return a value type, the method has a return value type
2 call timing is different: constructor: is instantiating new object is called directly and is called only once, and is an operation that is performed with the object creation
Normal method: Occurs after instantiating an object, through the object. Method call multiple times

Public TriAngle () {//construct a constructor without arguments

}
Public TriAngle (double b,double h) {
base = b;
height = h;
}
Public TriAngle (three) {
Three
}
Public the person (String name, int.) {
This (name, age, NULL);
//this.name=name; this.age=age; this.birthdate=null;
}

2. Description:
① If you are defining a class, you do not explicitly declare the constructor of the class. Then the system automatically gives the class an argument-free constructor. The
② how to declare a constructor for a class. Format: Permission modifier class name (formal parameter list) {}
③ if we define the constructor of the class once in the class, then the default parameterless constructor is no longer provided by the system.
④ The multiple constructors of a class, which make up the overloads of each other
⑤ summary: There must be a constructor in the class!

3. Ordering of attribute assignments in a class:
*① Default initialization-② explicitly initializes the-③ constructor by initializing the-④ through the "object. Properties" or "object. Method" method to assign a value to the property
When defining a class, you can set the default value directly for the property, but this default value is set only if the construction execution is complete, otherwise it will not be set
Example: Class Book {
Private String title = "Java Development";
Public book () {}//title now has a default value that is not related to this constructor

}
In this program, only the entire constructor is complete, the content of the "Java Development" string is actually set to the title property

Java constructor (constructor method)

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.