- Java 9 Programming By Example
- Peter Verhas
- 229字
- 2021-07-02 23:37:36
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.
- 深入理解Bootstrap
- CMDB分步構建指南
- 區塊鏈架構與實現:Cosmos詳解
- Apache Spark 2 for Beginners
- 編寫高質量代碼:改善C程序代碼的125個建議
- 精通API架構:設計、運維與演進
- 零基礎學Java程序設計
- 學Python也可以這么有趣
- Service Mesh實戰:基于Linkerd和Kubernetes的微服務實踐
- 零基礎看圖學ScratchJr:少兒趣味編程(全彩大字版)
- Python第三方庫開發應用實戰
- Java Web開發教程:基于Struts2+Hibernate+Spring
- 軟硬件綜合系統軟件需求建模及可靠性綜合試驗、分析、評價技術
- 網頁設計與制作
- RESTful Web API Design with Node.js