Let's not do the math the lifetime effort type I ordered on the program that we saw earlier in the introductory 2
lesson. 3
So it was simple run that program. 4
If you recall the class Nimitz class initialization Demel and it's only available for download from 5
the resource section. 6
So you can download it. 7
And are you going to add a new package called com not semantics cleared or JVM. 8
And that needs to be done within the demo project. 9
Recall that we now have two projects. 10
One is the demo and the other trio were three includes our real project code. 11
So any new code that we write in this chapter will go under this new package called JVM. 12
Now this class has been extended from what we saw in the introductory lesson. 13
This was done for the monster two additional things. 14
So let's just go ahead and review that and our program one more time. 15
That way we can also see the new stuff that got added. 16
So here is the class initialization demo and this is an instance initializer block and this is a static 17
initializer block. 18
So no changes there from what we signed the introductory lesson here are the new numerators that court 19
ordered. 20
One is get in. 21
Which is a static method. 22
On the other one is get in the file which is also a static method on both of them are simply retaining 23
some values. 24
Now we have the subclass. 25
No change there. 26
And it is extending superclass. 27
No change with that also. 28
And subclasses also implementing an new interface called Super interface. 29
So that's a new interface that we just added. 30
For this demo and we still have the two final few static final unstacked underscored final two. 31
So both of them are static final fields. 32
And one of them is a compiled and constant as the other is being initialized via a random function. 33
Now it still has the static initializer block on the constructor. 34
So no changes with that but a new instance initializer Rock has been added here and also a new class 35
called Object Reference has been ordered ironed. 36
An object of it is being created here. 37
So that's a plus. 38
Now let's move here to the superclass in the superclass so constructor is still there but a new starting 39
condition is a block and a new instance initializer block has been added here. 40
Now this is the object reference class. 41
The new object reference class which is initialized which is instantiated in the subclass. 42
And it just has one constructor and that's it. 43
Iron here is the super interface. 44
It has a static method. 45
So we know that static methods and interfaces are possible only from Java 8 onwards. 46
So it has this new static method and it also defines field as static final phone calls start to come 47
the final three. 48
And it is initialized in this way so we create an instance of class initialization demo and it has this 49
get in. 50
So we are invoking that. 51
And here there is a comment here. 52
This statement has been commented out. 53
So it isn't working to get into fine matter in the class initialization will solve it and we will uncomment 54
this and see what is with that. 55
And next is the main matter. 56
So Main and third is pretty much the same method. 57
So not much or not many changes. 58
So here we are printing. 59
Here we are accessing the static underscored final feed in the subclass underspending it. 60
So no change with that. 61
And here we are providing the static underscore final proofread and we are instantiating the subclass 62
which was also there earlier. 63
The only new statement as this. 64
Invoking the static method and later we will also uncommon this statement and we will see what it is 65
. 66
So that's the main method. 67
So no let's just go ahead and run this program. 68
But in order to see the lifetime of a type of this class. 69
So what we need to do is we need to pass an option call what class do the java command. 70
OK. 71
And the V it's an option that we are passing to the JVM. 72
And the way we do it is just right click and go here to run. 73
As earlier we were just clicking on this option jaywalked application. 74
Now let's get this thing run conflagrations and it opens this dialog click on arguments and here there 75
are two boxes here as the option that you can pass to the GBM. 76
Henri will call it as Marble's call in class. 77
And so this will be passed to the GBM. 78
And if you have any arguments here in program arguments those will be passed to the main method. 79
OK but we don't have any right now. 80
But if you just imagine that if we're running this whole thing from the command line and that is these 81
arguments would be inserted after the class name itself. 82
So use a Java class name on some program arguments. 83
But if you want to pass any arguments to the JVM then you said Java hyphen Werben colon class and you 84
would have the class named class initialization Demel and any arguments that we pass. 85
So any arguments passed here will be passed to the main matter. 86
So they will be assigned to those during oral argument that which is the only parameter that the main 87
method has. 88
Now I'm also going to include a pointer in the resource resources and that link will show all the options 89
that GBM would accept. 90
So you can also take a look at that that'd apply and not hurt her. 91
So now the class initialization demo has been executed. 92
Also just note that there are many classes here each of four each of them are docked last fall is also 93
generated on the on the desk. 94
OK so now let's just evaluate what we have here. 95
So let's begin with this. 96
And here as soon as the JVM has started GBM got created here the bootstrap classloader has kicked in 97
and it is loading this rt.jar Jotham jihadi did directory and we know that Arbi as Dornheim door ajar 98
and that includes the Kujawa library. 99
So now or here from here from this line on what it's going to load all the classes and the Kujawa library 100
that is all most of the important ones. 101
So you can see that the first one is the Djala that land or object itself. 102
Then here is a Java dart call that serializable. 103
Dot dot dot compatible on all of these classes and interfaces are getting loaded. 104
So that's the class loading process that it is done by the bootstrap class order can see so many different 105
class files. 106
Just go here and here at the very end is the class initialization them all which is the startup class 107
classes that we how we are running here which has the main method. 108
So this class is inside the JVM and that got it ordered. 109
OK so that's stuck only the class loading process. 110
So once that is done the GBM would actually initialize this class and that is happening here. 111
So the static in the initialization stage we know that the static initializer block rule will run. 112
So that's what is happening here is the static initializers block right here. 113
And we also have this related comment here saying that it's the initialization stage but the instance 114
initializer block. 115
Although we have it. 116
It's not. 117
It has not been executed. 118
The reason for that is an instance has not been created. 119
Here an instance of class initialization Demel has not been created. 120
It's just has been loaded and it just has been initialized. 121
OK. 122
Now after that after the static initializer walk is executed then the next thing that happens is the 123
main method is invoked. 124
So GBM actually invokes the main Materne and all the statements here get executed. 125
Now with each statement. 126
Something is happening behind the scenes. 127
And we are going to look at that. 128
OK. 129
So the statement is GBM in Rhogam main method. 130
OK so we are here in the main matter. 131
And the next statement is accessing this topic find free. 132
But this has been not in walk but decided not to load the subclass. 133
And the reason for that is that the client is a compile time constant so that value as has got copied 134
here. 135
OK so sapless has not been loaded yet and we are simply printing the value 47. 136
Now the next statement says in working subclause not static final do it's just printed AS-IS. 137
And here we are going to access Dodik finally. 138
And with that there are some interesting things that are happening. 139
So hosting this starting or is not a compile time constant So let's just go here. 140
So it's it's assigned disvalue value that I value by invoking this random function. 141
So what exactly is happening here and on if you see here. 142
So superclass subclass has got reordered now and because of this even super interfaces loader and super 143
process roaded so because it's extending Supercross and implementing super interface and they are loaded 144
in this particular order Moka interface first and then the superclass then the subclass after these 145
three got rewarded then the static initializer block in the superclass has been that which means the 146
initialization off superclass has completed and that's the reason the static condition is Godkin superclass 147
got executed here. 148
And that statement is getting printed here. 149
So you just go here to the superclass. 150
Here it is. 151
So this it meant got executed but the super interface is just loaded. 152
But it's it is not initialized. 153
So if you recall that's what we said when we discussed initialization for what an interface it gets 154
initialized only if it's static method is invoked or if it went off its use is invoked and that field 155
has one off the field is accessed under you cannot be a contes compile time constant only one of those 156
two things happen. 157
So an interface can will be initialized. 158
So that's the reason an interface is only has only been loaded but it was not initialized. 159
OK so we see this statement but following that we have these three lines here. 160
So that is because so it's hurting the mat something with some something related to my class. 161
That's because here are in the subclass we are invoking the random function. 162
So some classes are related to that are getting loaded here and what it means is it's lazy loading. 163
OK. 164
So these things are encountered now ironed and they are being lazy ordered. 165
OK. 166
So after that the start. 167
So though the superclass has been initialized now the subclass is getting initialized and the static 168
initializer grock in the subclause got initialized underspent in the statement on Finally the stocktake 169
. 170
Finally you got assigned a value of zero in the main method. 171
So that's that. 172
And after that we are saying instantiating subclassed and we are creating an instance of a subclass 173
. 174
OK. 175
Now when we instantiate this subclass what happens is. 176
So we need we need to loop this subclass but subclass is already loaded Onitsha in the memory on the 177
heap. 178
So it's not going to get loaded once again. 179
So the classloader will simply use the class object class which is already in the heap for subclass 180
and it's also going to do the same for superclass. 181
In fact we know about constructor chaining. 182
So because of that the superclass constructor will be in Rockfest before the superclass constructor 183
is invoked the superclass instance initializer will be in work here. 184
Harridans supercross because an instance of superclass has been created and it's following that the 185
constructor is in work. 186
So that's the construct of changing so fast the constructor of the superclass is invoked before this 187
subclass constructor. 188
So that's why you see these two statements. 189
And then we the control goes into the subclass that tries to initialize all the instance variables and 190
also runs the constructor call. 191
But here we have this variable here object reference which is where we are instantiating this object 192
reference class. 193
So that's the so the lines that you see here is really is related to that. 194
So this is something to do with that. 195
And here we are loading the object reference class and the object reference constipate results are in 196
Wilker following which subclass in the initial instance initializer unsuppressed constructor are invoked 197
. 198
Now here interesting thing is subclass. 199
But here if you see the output here instance initializers statement is printed before the constructor 200
. 201
But if you look at the code instance initializer is appearing after the constructor in the source corner 202
. 203
So and that's because instance initializer is always copied to the beginning of constructor by the compiler 204
itself. 205
So compiler is doing that class. 206
OK so that's about that. 207
And let's look at the next statement here the static method is being invoked. 208
Now the super interface will be initialized. 209
So let's see what we have here. 210
OK so it's calling the super interface is getting initialized. 211
So because of this invocation of the static method Sinaloan Let's go into the Super interface model 212
. 213
So when we say it's initialized so fast this field gets this treatment gets executed. 214
And here this class instance is being created instance of class initialization demo which means that 215
any instance of variables or any constructor or initializer within this class will be executed. 216
So that's why you see that the instance initializer within the class initialization do more has got 217
executed under. 218
And we're printing that. 219
And after that the GET IN method is getting in walk. 220
So that's what is happening. 221
And finally after that the static method indecl put interfaces being in walk. 222
OK so this gets initialized only when a static method is invoked or when a field is invoked which is 223
not compile time constant. 224
So for that let's just go here and let's uncomment this. 225
So instead of static method now we are accessing the static final three which is a field and super interface 226
. 227
So if you go there. 228
Here it is. 229
So now let's run this one more time and let's examine this final code. 230
So the class initialization demo has been initialized in the instance initializer is run and the get 231
and method is invoked. 232
And the start declining three values assigned the studio. 233
So the super interface is one more is once again initialized not uncommon. 234
This also and let's run it one more time. 235
So here we have two trees on both of them. 236
One of them is calling the get end method in class initialization Demel and the other step on the other 237
field is in we can get in five. 238
The second matter. 239
And this. 240
But over here if you see we are in we're accessing only one of the rooms start to underscore final treat 241
. 242
But that is also because this class interface is being initialized. 243
It's also executing this second statement. 244
Static underskirt final 5 is getting initialized because when it gets initialized it's going to initialize 245
all the static final FIERS. 246
OK. 247
So that's why you'll see that when when the instances are being created in the two statements that's 248
where you see that your class initialization the more instance initializer is run once ungrudging get 249
in has been invoked on here instance initializer in the second statement. 250
Due to the second statement it got executed. 251
Get in 5 is getting involved this time. 252
So as long as one of the fields is accessed which is not a compile time constant then an interface will 253
be initialized and all the rest of the Foose static fruits will also be real will also be executed. 254
So that's about the interface initialization. 255
So that's about it. 256
But finally if you just think about it you may feel that there is not much value in learning on this 257
. 258
For example in the main method we're accessing this topic you start to underscore find them unstarted 259
final two from the subclass and all the kid is whether we are able to access those values or not. 260
What difference would it make if we know whether those classes are loaded uninitialized or not. 261
So that's a valid thought. 262
But the thing is we want to do better than that. 263
We want to know what the Java platform is doing internally. 264
So we are pushing our boundaries here on trying to get a better grasp of the technology. 265
And that's the reason behind having this chapter in this course but it's not just about making our self 266
complete. 267
But the designers also have a reason behind making making it work in this way. 268
For example there is a reason behind having constructor chaining. 269
You want to pretend to be initialized by fortune. 270
Otherwise there can be issues. 271
OK so there can be some issues on so it's important to be aware of how things are handled by the platform 272
. 273
OK so it's a good thing that we are studying all this. 274
So that's about it. 275
Hope you enjoyed this remote. 276
See you in the next lesson.