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