Summary of considerations for Java construction methods

Source: Internet
Author: User

Construction Method Details Summary ~~~~~

1: First to understand why you need to construct a method ,, there are too many properties in the class, each time to assign a value to the property is very troublesome: the encoding is large, you cannot reuse the code to assign value to the property.

2: What is a construction method?

The constructor method is responsible for initializing the instance variables in the class.
The constructor method is a special method that must satisfy the following syntax rules:

A: The construction method must be the same as the class name, and does not contain a return value type Description section.
B: The construction method cannot have static and final keyword adornments.

3: How do I use the construction method? call construction method using the New keyword

4: Display construction method and implicit construction method

  When you create a class, if you do not explicitly define a construction method, there is a default parameterless construction method for that class;
You can declare one or more arguments in a class, but each construction method has a difference in the number of arguments or parameter data Types!!!

!!!!!!!!!!

  If an explicit construction method exists in a class, the default parameterless construction method will no longer exist unless you explicitly define an parameterless construction method

There can be more than one constructor in a class, the constructor can be called through this implementation, but the calling constructor code must be written in the first line of the valid code, which is very important oh ~

1  Packageconstructor;2 3  Public classApple {4 5     PrivateString name;6     PrivateString color;7     Private DoublePrice ;8     9     //Non-parametric construction methodTen      PublicApple () { One          A     } -  -      PublicApple (String name) { the          This. Name =name; -     } -      -     //multiple constructor methods, which are identical to the type of the parameter and the type of the parameter in the constructor method when called +      PublicApple (string name, string color,DoublePrice ) { -          This(name);//the call in the constructor method is called with this (parameter), but note that a call to another constructor method can only be made once in a constructor method.  +          This. color =color; A          This. Price =Price ; at     } -      -      Public Static voidMain (string[] args) { -Apple apple1=NewApple ("Apple"); -Apple apple2=NewApple ("Apple", "Red", 12.5); -     } in      -      to}

Summary of considerations for Java construction methods

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.