Instance initializer block
Que) What is the use of instance initializer block while we can directly assign a value in instance data member? For example:Why use instance initializer block?
Example of instance initializer block
Output:speed is 100 speed is 100
What is invoked first, instance initializer block or constructor?Output:instance initializer block invoked constructor is invoked instance initializer block invoked constructor is invoked
Note: The java compiler copies the code of instance initializer block in every constructor.Rules for instance initializer block :
Program of instance initializer block that is invoked after super()Output:parent class constructor invoked instance initializer block is invoked child class constructor invoked Another example of instance blockparent class constructor invoked instance initializer block is invoked child class constructor invoked parent class constructor invoked instance initializer block is invoked child class constructor invoked 10 |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263830.html