官术网_书友最值得收藏!

Object initializers and constructors

When an object is instantiated, the appropriate constructor is called. The constructor declaration looks like a method with the following deviation: the constructor does not have a return value. That is because the constructors work on the not-fully-ready instance when the new command operator is invoked and does not return anything. Constructors, having the same name as the class, cannot be distinguished from each other. If there is a need for more than one constructor, they have to be overloaded. Constructors, thus, can call each other, almost as if they were void methods with different arguments. However, there is a restriction—when a constructor calls another, it has to be the very first instruction in the constructor. You use this() syntax with an appropriate argument list, which may be empty, to invoke a constructor from another constructor.

The initialization of the object instance also executes initializer blocks. These are blocks containing executable code inside the { and } characters outside the methods and constructors. They are executed before the constructor in the order they appear in the code, together with the initialization of the fields in case their declarations contain value initialization.

If you see the static keyword in front of an initializer block, the block belongs to the class and is executed when the class is loaded along with the static field initializers.

主站蜘蛛池模板: 泌阳县| 定襄县| 安泽县| 丰原市| 固镇县| 荥经县| 微山县| 北流市| 林州市| 保德县| 扎赉特旗| 仙居县| 巴楚县| 博野县| 洪洞县| 津市市| 集安市| 涪陵区| 图们市| 平武县| 盐亭县| 和田市| 尚志市| 莱西市| 文昌市| 云南省| 府谷县| 贡山| 乌恰县| 唐山市| 康保县| 安福县| 贵德县| 柘荣县| 普安县| 呼伦贝尔市| 寻乌县| 西吉县| 嘉义市| 西畴县| 温州市|