It does them all for the affliction. 2
We have two classes. 3
One is called the refl. demo and it is a main class on has been appropriately named I and the other 4
one is called as a user of dot Jawa. 5
So both of these classes are either in the GBM package so refl. do more as a main class and written 6
refl. them or we have the main method. 7
And there are things that we are testing here. 8
So there are two methods for Budapest's one is called as demo class object access. 9
So as the name suggests this particular method is used to demonstrate accessing class object so there 10
were three ways to access class objects. 11
Unreal. 12
And we'll be looking at all three of them and we will also see how the Mad-Eye information in the class 13
object can be accessed. 14
And the second method is Karlos demo code reflection. 15
And here we will explore more reflection properties for reflection features. 16
So we will demonstrate instance creation and method invocation using the class object. 17
So the class object. 18
Here is the input. 19
So that's what we are going to test here and use and our July is pretty simple. 20
We are already familiar with this class so it has all these fields and it has no constructor and it 21
also has this constructor which has all these parameters on initialises the fields and there are two 22
methods. 23
One is called a save weblink and the other is both bookmark review with some parameters here. 24
But let's not worry about all of that because that's not relevant here. 25
And just wandered into Boullion. 26
So let's go back. 27
So both these classes are available in the resource section for download. 28
So let's just look at what we have in each of these matters. 29
So let's just go here first with a demo class object access. 30
So the first one as accessing the class object via object reference. 31
So for this we are defining this string array object and it has these three values. 32
And all we are doing is just invoking since a and is also an object in Java we are invoking the get 33
class method and we are just simply printing or to get name. 34
Now the second thing we are testing is the Arctic farming method and the class. 35
So we are passing the fully qualified name here. 36
After that off the user class which is also in JVM. 37
So we are passing the fully qualified name on we are loading the class and we are printing all the information 38
at least some of the important ones like what did we miss with that it's an interface or not. 39
If there are any interfaces that this particular class user is in stone is implementing. 40
And also the superclass here getting the superclass off user and printing its name. 41
OK. 42
Now the third one here there is one additional test that we are doing. 43
We know that Glassdoor foreign for named does not work with primitives. 44
So here we have a primitive here here and this word at runtime. 45
This would throw an exception called class not one exception as was discussed. 46
So the congruity comes here and brings this message. 47
And finally there Doc class the class nutritious approaches tested here so we ain't dog class which 48
is a class object and we are simply printing the name. 49
So that's the first method and here is a second letter to the second method we are placing a class object 50
here. 51
So we are getting the class object using the foreign name approach and we are simply passing it here 52
. 53
And all we are doing is we are creating a new instance here. 54
So on the class object this object we are calling this method call new instance. 55
And in order to do that the user class must have a default constructor and that's the reason for having 56
this constructor here. 57
It should be there. 58
Now once we do that we are invoking this method Corylus get declared methods and it contains all the 59
methods in the class. 60
OK. 61
And so I couldn't send off all the method objects or here and method object is from the dot dot Langhart 62
reflect package. 63
And we are printing the names. 64
And here we are making use of the white glass object. 65
So we are simply saying why not glass. 66
And if this particular method in the in the current iteration if it has we are calling the return type 67
method and get returned by returns that the class object corresponding to the return type and if it 68
equals the white class or a wider class which means that the class object responding to why don't we 69
simply print dispatch it message method or can type as wide. 70
So one of the methods has a void. 71
It can type on in here get declared constructors. 72
So disorder gets on the constructors and each construct it is basically an object. 73
Once again from the Java Langhart reflect. 74
And here we are getting one particular constructor. 75
The constructor that does that for fetching is based on the number of barometers and the type of the 76
parameters and basically the be on the type of the parameters. 77
So if you look at this etches get declared constructor has an array of class objects are being passed 78
here. 79
OK. 80
So it's a virus parameter. 81
So based on that we get the constructor that we need and then we are creating a new instance of that 82
. 83
And we are passing all the values for example here it is anti-abort glass. 84
The first parameter so we are passing this value 101 on the second parameter is name that Hurleys email 85
. 86
And so we are passing all the corresponding values that we need to pass. 87
And here we are fetching a method and invoking it. 88
So previously we are fetching a concept around in working it. 89
Here you are fetching a method only milking it. 90
And all we do is we in the matter get declared method on the class reference variable and we are saying 91
that we need this method call see weblink and we are also specifying the parameter types here. 92
String not grass. 93
Another one is string dark glass. 94
So if you go back here has string parameters. 95
So that's what we are saying we need that particular method. 96
OK so this get declared better. 97
As for getting one particular matter get declared constructor as for fetching one particular constructor 98
and the way you walk and method is. 99
So you have the method object here just call in matter and you need to pass this user object. 100
You need to pass this use object that was created earlier and we need to pass the values also. 101
So we are passing. 102
Do I use one of them as the host name and the other one as say rebeling is some idea. 103
Sorry that you are an Teicher. 104
So you're passing those values here. 105
And this is returning some value. 106
So Doc ralliers are off and working that is being printed here. 107
So that's about it here. 108
So let's just go ahead and run this program and look at the values too. 109
So let's look at this. 110
So here it is. 111
We are printing this. 112
This is using the Glasman getting the class object we are the object reference and object reference 113
is basically a string array. 114
So it's printing the name. 115
So this is the name that's getting printed. 116
So since it's an array single dimension an array we have this squared record. 117
And since it's a string that it contains. 118
So we have this string class preceded by an L as mentioned earlier and a semicolon. 119
So that's the first one. 120
Next we are using the start for a new method. 121
And we are leading the class here and we are printing the name here. 122
Class not get me. 123
Which gives the name of the fully qualified name off the user class. 124
Is it an interface. 125
No number of interface it implements. 126
No it doesn't implement any interfaces. 127
And what is a superclass superclass is there is only one class which is it's not excluding any other 128
class. 129
So the only class it extends as the object bus underspending its name here. 130
Now here we are trying to load a primitive using for name which we know doesn't work. 131
So the control goes into the catch block here and brings this message glass not found exception to class 132
dart for me not get me. 133
So that's just one type of message. 134
And here we are printing using the class to access the class object and we are printing its name. 135
And here it is. 136
It just prints it. 137
That's it because it's a primitive. 138
Know let's move into the second method which is a demo called reflection. 139
And here a new object is created. 140
And so the object name the variable name is object. 141
And we are getting we are getting it's get using get Glasby and getting the class object and we are 142
once again simply printing it. 143
OK. 144
So for I'm the class object we are creating an object. 145
But then from the object we are getting the class object one more time. 146
And just printing its name just to demonstrate all of this. 147
So if you go down here which says object don't get class as this is the name of the class of this particular 148
object. 149
Now let's. 150
Next as we have printing out all the methods shown here are declared methods and they are ignoring the 151
get named method here. 152
So it's printing to matter say a weblink was bookmark review. 153
OK. 154
And one of the methods post-Bush Marco Rubio has 155
a right as the return type. 156
So it goes into this if condition this if statement executes. 157
It has a true value and it prints this statement. 158
Next is we are printing all the constructors and we are breaking the name. 159
Name is simply the user here for both of them are numbered alphabet I mean girls. 160
So there is a method called Get barometer types on in where we are invoking that document. 161
So on the first iteration or just the default constructor and the second it's the second constructor 162
which had five parameters. 163
And finally here we are invoking the method we are creating an instance using the instance of user using 164
it using this constructor and then we are also getting though the method. 165
This particular method called Save eyeblink and invoking it but passing the user instance here. 166
And so it has this particular method has some print statements on it's getting printed and simply printing 167
older the values passed here the parameter values. 168
And finally the result of invoking save eyeblink that it can take is basically a boolean and it truly 169
is getting printed here. 170
So that's about that's about the reflection the more that we have. 171
And one thing I want to mention as a reflection is definitely slower compared to non-HD reflection court 172
. 173
So for example if you're creating an all if you're creating objects using reflection and if you also 174
compare it if you create the same object be it using non-HD reflection. 175
So you're using the new Nuki keyword then definitely in the non-definition code will be slightly faster 176
but a reflection is also useful. 177
It needs to be used but it's just that it is a bitch. 178
It is kind of slower. 179
And so you want to be really careful if your code is going to be frequently used and you have and you 180
have the reflection code there. 181
So if it is being used in a very frequently used code and application or surface performance intensive 182
then you need to be careful about that. 183
Maybe you don't want to use Reflection in those parts of the code. 184
So so just to exercise caution be careful. 185
OK so that's about it. 186
So that's refl. demo and we have a strict pretty much everything that we have learned. 187
Thank you. 188
And happy coding.