Java Construction methods

Source: Internet
Author: User

Construction Method: Literal Explanation: The method that executes synchronously when the object is constructed in new (the method name needs to be consistent with the class name). Function:1initialize a class member variable: eg:classA {intOne ; intboth ;  PublicA () { This. one = 10;  This. both = 20; //A is the constructor of ClassA, which assigns the member variable one and both values to 10 and 20.                     }                }        2support for multiple construction methods, in overloaded form (depending on the parameters passed in, choose a different construction method)classB {intOne ;  PublicBintmath) {                         This. one =Math; }                        //Pass in an int parameter, that is, this construction method                                         PublicBintMath1,intmath2) {                         This. one = Math1 +math2; //Pass in two int parameters, that is, this construction method                        }                                    }        3. The constructor method can not only initialize the member variables (in fact, this method is the same as the normal method, but the construction method can be run when the object is new, but the common method requires "object. Method Name"run on Call)4. Add: In fact, each class must have a default construction method, do not need to write and have ("public class name () {}")        5the construction method differs from the normal method:1. Format different constructor method: Modifier class name (parameter type parameter ...) {Initialize member Variable} General method: Requires a return value type2The function of different construction methods is generally used to initialize member variables;                The general method depends on the demand; 3Called when the object is created by different constructor methods, or this ()Super() statement calls a normal method requires an object call or a static method to invoke a static method directly. 4execution of different construction methods is performed at the time of object creation and is performed only once. The general method is called by the object after it is created, and can be called multiple times. 

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.