0

Class methods always executes earlier than constructor's method? Why sequance is 2143?

https://code.sololearn.com/cb8hpCuOSOhs/?ref=app

23rd Nov 2019, 1:44 PM
Даниил Ефремов
Даниил Ефремов - avatar
3 Respostas
+ 6
Даниил Ефремов Instance Initialization blocks are executed whenever the class is initialized and before constructors are invoked. They are typically placed above the constructors within braces parent class IIB is invoked first then normal class IIB And these are the ones which are executed before constructor function invocation These are the normal methods of a class (non static), you need to invoke them using an object of the class https://www.geeksforgeeks.org/instance-initialization-block-iib-java/amp/
23rd Nov 2019, 2:04 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 1
To add on.. static block executes even before IIB , but since it is static it will be executed only once.. Ex: static { //something... }
23rd Nov 2019, 2:07 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 1
Always remember that the super() is called first, then comes your instance initializer block and then the actual constructor is executed. When you create an instance of SomeClass2 then the constructor is called and which through super() calls the constructor of SomeClass which also has a super() calling the constructor of object class which does not return anything. So now the initializer block of SomeClass is executed then your SomeClass constructor. So you have 21 now. Now the initializer block of SomeClass2 is executed and then your SomeClass2 constructor. So you have 43 now. So this results in output 2143.
23rd Nov 2019, 2:12 PM
Avinesh
Avinesh - avatar