In the last lesson in that the more we have seen that we can run into issues during the D.C. legalization 2
process. 3
So there is something more we need to know about D.C. validation process and that's something that we 4
will see in this lesson we will also see how we can avoid the issue that we ran into during that Emel 5
And here's the DC that can process. 6
So we first read the serialized object and in the process GBM also tries to lower the corresponding 7
class object. 8
The class name of the serialized object will be stored with the serialized object itself. 9
And so JVM looks at the class name and will try to load the corresponding class know D.C. delegation 10
will fail. 11
If for some reason the class cannot be loaded it could be because the GBM could not find the class file 12
. 13
Or it could be due to some other reason to. 14
But if the class could be loaded successfully then as a second step GBM tries to compare the motion 15
of the loaded class but the notion of the serialized object and the need to match a device it means 16
that the class has evolved after the object was serialized and they are no longer compatible and due 17
to this JVM with an exception NDC realization will fail. 18
And this is what happened during the demo when the last lesson but if there is no motion mismatch then 19
GBM would create space in the heap for the serialized object and recreate the object with the same state 20
. 21
So it's going to run that can strip it off the object because if that is done then it means that we 22
are creating a brand new object that by Newsted and that is not what we want what we want is to recreate 23
the object with the same state that the object had when it was serialized But if there is any norms 24
Serializable ancestor in the inheritance tree of the serialized object then that ancestors construct 25
it would be done along with any constrictors of all it's direct or indirect superclasses under super 26
process maybe Serializable do. 27
And that wouldn't matter so conceptus would run starting with the first Nahm Serializable ancestor unbe 28
already know this as part of restoring state. 29
Instance variables will get serialized did while Crunchie and variables will get default values so that's 30
a B.S. legalization process. 31
Let's not look at more specifics about worshipping every Serializable class gets a version ID which 32
is computed from the classic structure on this motion ID is stored with that dot Busfield know it and 33
the plastic took genius the Russian idea also genius on this motion ID has a name under the name Das 34
serial quotient you ID as an idol of this Light That Is it as a bush an ID of a serializable class. 35
So sort of wish an ID is specific to only Serializable classes on Hence we have the term CDO in the 36
name of the ocean ID. 37
So this motion ID will not be gender dered for non-C realisable process know in a seat an object is 38
generated then the object is time. 39
But the motion IAB that the class had in that instance of by and during B.C.E. realization I spark off 40
Worsham comparison devotionally of serialized object will be compared with the Russian idea of downloaded 41
to us so if the class has evolved after the object was serialized then obviously motion ideas were mismatch 42
. 43
Transition. 44
And this is what happened in the last Dynel So the question would be how can we make D.C. realization 45
to succeed even when the class has a world after serialization and the solution is to make the version 46
ID constant even if Grassley wants NBC how that can be done. 47
Let's get back to our demo cook here we are in the I will do more class once again and we're going to 48
continue with where we left off. 49
So in the last day more we made this change uncommented gender. 50
So the class has evolved unbid that we had a version mismatch NDC realization has failed and we just 51
mentioned that in order to resolve that we need to keep the and the constant so that can be done by 52
declaring a constant CDL Worsham new ID as you can see here since it needs to be a constant. 53
It has marked dust not big on final and it has to be long. 54
On here as a senior Beauchesne you ID and we are using the name as it is. 55
And it is assigned this value. 56
And the question is can we get this value from on for that Java library comes with a tool called Silver 57
. 58
OK. 59
And we are going to use that silver and we are going to pass. 60
We are going to run that tool and we are going to pass this in can be passed on in the movie then we 61
get done serial version of this particular class. 62
So in order to do that let's just go back and start from the beginning. 63
So let's just call this art and just keep this as it is me open my command prompt here. 64
So I am and gee I read them all. 65
So there is the current project and I'm in the bin folder because in the bin for dinner itself we have 66
the classifiers OKB in the demo we how both SIRC unbent and within the bin. 67
We have the actual classifiers. 68
So we need to go into the bin folder. 69
Let me just run this command. 70
So it's called Silver and we are running it on the Iodo more dollars Serializable Demel because you 71
didn't as a builder I was a nested class. 72
We can access it this way. 73
This silver is being is it is being run on that docked classified. 74
So if you look into this folder I will falter here you will see I will do a moderate class as well as 75
as well as I order more dollars Serializable Demel and you will see both of them. 76
So we are running the seedier version on this particular Serializable nested class because we want that 77
serial quotient ID. 78
So on this if we run it. 79
So this is what we get and it's already here. 80
So it's just the same number. 81
So I'm just going to uncomment this. 82
Now let's just go ahead and say Run. 83
Run configuration. 84
Recall that we need to pass through here in order to serialize the object. 85
So we are going to start from the beginning and I'll say apply here and run it and I go back there more 86
and if I hit refresh it will see dark. 87
That is the CD a lot easier. 88
OK so Dr. template has done seat on this object now. 89
I mean just go back so let's not involve the class so let's just do this Bartletts not regenerate there's 90
not that research realize that blasts so let's go back and do it on configurations into arguments maybe 91
modus So with this only desalinisation Isbel form and now you can see that despite uncommon doing this 92
we did not get the exception that we had earlier. 93
OK. 94
And that's because we how hard coded the Russian ID. 95
So even so the object serialized object has had this notion ID but the class has evolved and we hardcoded 96
the ocean ID. 97
So when we run as a serial version let's run it once again so with this is with no change and you will 98
see that it still has the same bush an ID. 99
OK. 100
So we are hardwiring the Russian idea here. 101
So that's how you can ensure that because your position does not fit. 102
But there are some changes and if you make those changes. 103
Utilization still fit. 104
For instance let's do this let's just make it NC realisable OK. 105
No actually if you run version ID it will not be when See it says that it is not serializable because 106
in order to because there is no emotion or devotion I doubt you will be generated only for Serializable 107
classes. 108
Q So the old version but also say that it cannot. 109
It cannot. 110
It cannot give us any question Id. 111
Now that's just go ahead and run this. 112
So with this change we will once again get this error which means that ABC Television has it. 113
So there are certain changes that we can make on certain changes that we can not make. 114
OK. 115
So let's head over to our site and see what changes we can make and what we can not make. 116
OK. 117
And this class as available and the resources section on you can check it out on download on book critic 118
so let's just go back to our sleights 119
unhidden the changes that do not affect D.C. realization on most of these are about instance variables 120
you can add or delete an instance variable to the class and that will not break. 121
Between the audition process if you add an instance variable it will get the default value. 122
You can also make an instance variable static you can change a transient variable and go nonconstant 123
and the variable would still get the default value good NBC television you can also change access levels 124
off the tables and duckbill don't have any effect. 125
Finally you can all add classes to the inheritance tree and you can also REMAUT classes from the inheritance 126
tree and that wouldn't affect the D.C. delegation process. 127
And these are a couple of changes that do affect DC that position you cannot change the data type of 128
an instance variable. 129
We have to see the second one in our demo. 130
That is you cannot make a CD and as a bluegrass non-C then visible but this rule is not for just the 131
class itself but if the class includes one or more object graphs then none of the Serializable classes 132
that dingo's object graphs can be made on Serializable So even if one class is made Nancy Serializable 133
within an object graph then it would break the DCD tradition process. 134
So that's about it. 135
I would also suggest that you should try out all these genius in code and see the results for yourself 136
. 137
So to summarize serialization is useful for saving regular Java objects to devices like desk to recreate 138
Java objects from their serialized form people from DC theorization and B use object output stream an 139
object input stream process four-CD elevation NBC idealisation finally if you want the classes to evolve 140
without affecting DC or they can then you can define a constant Silverbush an ID. 141
But you need to bear in mind that DCR addition can still fail with certain genius so that's about it 142
. 143
Hope you enjoy learning about serialization. 144
And thank you