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

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.

主站蜘蛛池模板: 阳东县| 岐山县| 石屏县| 富裕县| 溧水县| 婺源县| 通辽市| 和田市| 玉屏| 沙河市| 阳曲县| 两当县| 合水县| 昭平县| 崇义县| 新余市| 桦南县| 平山县| 勐海县| 长乐市| 东辽县| 乌鲁木齐县| 临西县| 邛崃市| 无为县| 莒南县| 定兴县| 夹江县| 伊春市| 申扎县| 利津县| 大关县| 杭锦后旗| 彭州市| 白河县| 郯城县| 普安县| 遂平县| 邛崃市| 宜宾市| 洪雅县|