We know how bigger and instance variables are declared or initialized. 2
However Djala also provides a facility called initialiser block which can be useful for initializing 3
tactic instance variables in certain situations. 4
There are separate initialiser rocks for instance variables. 5
And let's see what they are. 6
Let's just look at static initializer. 7
It can be useful to initialize static fields that cannot be initialized in a single line. 8
That is you need multiple lines to initialize them and here are two good use cases. 9
First one is about populating a data structure. 10
Well second one is about having some error handling code if initialization leads to any errors. 11
Let's look at examples for these two use cases in this example. 12
We are creating an instance of hash map which is a data structure that we will review later but it is 13
basically a hash table implementation that stores a collection of key value pairs. 14
Example would be starting a persons name esky and his or her phone number as a value. 15
It wouldn't help us to search for a person's phone number given his or her name. 16
So here we are first creating a hash map object which is referenced by a static variable. 17
Now if you want this hash map to be populated with some data before it is used anywhere in the class 18
then a good way to do it would be to use a static initializer block that is you would populate the data 19
inside the static initializer block as we can see here the starting condition as a block involves the 20
keyword static followed by opening and closing lists and in between the two phrases we have the statements 21
populating the hash map. 22
As you can see we're adding the key and value pairs in each statement. 23
So the starting condition is that block is helping us to completely initialize the hash map as it could 24
not be done in a single line. 25
When I say completely I mean even populating the hash map with this no any code in the class can use 26
the popular hash map. 27
Here is another example. 28
Let's say there is a static variable called stuff and it is initialized by the value written by this 29
method called Get Stuff No get out of control an exception and exceptions will be covered later. 30
But as we mentioned earlier it is basically an error. 31
And we may have to handle it when it is connected. 32
Destroy catch but that you see here is used for catching exceptions and handling them and certainly 33
the exception handling Guard can go into this catch block. 34
Not without a static initializer. 35
It wouldn't be possible to have this initialization statement and grossly in a quick catch block directly 36
within the class. 37
That is we get a compiler error. 38
So static initializer is permitting us to do this. 39
Alternatively we can use a private static method as shown here. 40
Here the static variable is initialized been in rockish an affair. 41
But I read static method that one did of this approach is that we can always re-initialize the start 42
degradable by simply invoking this method. 43
We cannot know how any number of static initializers and they will be executed in the order in which 44
they appear in the source code. 45
Like static matters Cordone search static initializer cannot reference instance members like Instance 46
variables are instance methods and that's about static initializers. 47
Let's look at instance initializers an instance initializer basically initializes instance variables 48
. 49
And here is the syntax. 50
It is same as static initializer but not start a keyword. 51
But we know the constructors initialized it. 52
Then why do we need instance initializers. 53
It is useful when sharing a block of code between multiple constructors. 54
What it means is that under the hood the Java compiler copies initialiser Brockes in the beginning of 55
every constructor. 56
And without this feature you would have to manually copy the same code in each constructor. 57
Let's quickly try this out in code. 58
Ok here is the basics demarc class. 59
So let's just go ahead. 60
I had an instance initializer here. 61
Let's just have a print statement. 62
Let's just print the statement inside an instance initializer 63
. 64
Now let's also add a constructor 65
. 66
And this Conard has no our constructor. 67
Let's add one more constructor that some fake parameter ID and purchase say inside constructor regret 68
barometer. 69
OK let's simply invoke create an instance of BASIX them or just call it bt where the new is X Nemo. 70
So let's just compile run it so as you can see it says inside instance initializer and then it says 71
inside nor constructor. 72
That's because we are invoking the constructor here. 73
And as you can see the instance initializer code has been copied to the beginning of the constructor 74
and it also appears after the constructor so it doesn't make any difference so it's just going to copy 75
this into this matter into the into the constructor into the beginning of the constructor. 76
Now let's just pass a value on here so that we are invoking the second constructor and so that to compile 77
once again. 78
And let's just run it. 79
So now as expectorate is inside instance initializer and then it also says inside constructor with a 80
parameter. 81
So once again the instance initializer has been copied to this to the beginning of the second constructor 82
. 83
So basically instance initializer code will always be copied to the beginning of each of the constructors 84
. 85
So that's the main use and we have seen it here in code and that's about it. 86
No like in the case of static initializers we cannot also how any number of instance initializers and 87
there will also be executor in the order in which they appear like instance Medders instance initializers 88
can also reference static members and that's about it. 89
We'll look at buttstock an instance initializer groks static initializers Grogs Halvard initialization 90
of static variables is not possible in a single line instance initializer. 91
Blocks can be used when multiple constructors have some common initialization called and that's about 92
it. 93
Thank you