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

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.

主站蜘蛛池模板: 甘孜| 读书| 谢通门县| 罗田县| 彭阳县| 页游| 扎囊县| 肇庆市| 建宁县| 廊坊市| 泰宁县| 清镇市| 东丽区| 洛宁县| 搜索| 婺源县| 交城县| 玉屏| 南部县| 满城县| 射阳县| 嘉祥县| 句容市| 昌宁县| 尖扎县| 舞钢市| 永兴县| 金溪县| 丰顺县| 夏津县| 景谷| 闸北区| 图们市| 定州市| 凌源市| 玉环县| 崇仁县| 牡丹江市| 柳江县| 左贡县| 南召县|