0
Class methods always executes earlier than constructor's method? Why sequance is 2143?
3 Antworten
+ 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/
+ 1
To add on.. static block executes even before IIB , but since it is static it will be executed only once..
Ex:
static {
//something...
}
+ 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.