So Djala API and how they are arranged in two packages Next to know is how do we access the classes 2
in those packages so that we can use them in our own classes. 3
It's not just about standard Java API or some third party API but almost all projects will generally 4
include one or more packages unless we are writing some really small programs. 5
So we need to know how to access classes and packages. 6
And that's what we're going to learn now. 7
At the end of the lesson there'll also be a demo of most of the stuff we have learned in this lesson 8
. 9
It is very common for classes within the package to access other classes in the same package in such 10
cases classes can directly reference those other classes by just using their class names. 11
But if we have to access a class from a different package then there are two ways to do it. 12
Firstborn is by using an import statement. 13
And second is by using a fully qualified blastin a fully qualified class name is nothing with the package 14
name followed by a dot which is then followed by the class name. 15
Normally we always use an import statement. 16
But there are certain special scenarios where you may have to use the fully qualified class name in 17
this lesson apart from looking at importunate men. 18
We will also look at the scenarios when we would have to use fully qualified Glaston also know that 19
these methods are used regardless of whether or not the package belongs to an external API that is even 20
if a package was written by us in our own project we would still use these methods to access the classes 21
in that package. 22
Hustle let's look at import statement using a simple example. 23
Let we look at the fully qualified placename approach. 24
Let's say we want to use a class called idlest which is part of the job Rodarte you do package this 25
we can use an import statement as shown here. 26
That is a statement involves the key word import followed by a fully qualified name of the class which 27
is most a package name Jawad article followed by a dot which is then followed by the class name. 28
Idlest this informs the compiler that we want to use the class ironist from the package. 29
Tarkington. 30
Not that the statement must be at the top of the class outside the class definition having it inside 31
a class definition or after class definition is invalid. 32
So once we have the import statement in place we can use our list anywhere in the class as you can see 33
we are creating an idealist object that in the method foo had we not included the import statement this 34
statement would have given a compiler error. 35
In the example we just saw re-imported a single class called ironist from the package Jawad Arterton 36
the import statement used is referred to as an explicit import which is also referred to as single type 37
import single type imports such as that we are importing a single type and that type can be either a 38
class or an interface. 39
An interface that will be discussed later but often you also put multiple courses from the same package 40
. 41
In that case there are two options. 42
One is of course to have an explicit import statement for each class to be imported. 43
The second option is to use something called starting work which is also referred to as input on demand 44
. 45
Get started work start import you would simply include an asterisk following the package. 46
And this will import all the classes in the package in this example all the classes in Java that do 47
package are imported on Hence the class having the statement can use any of those classes so start import 48
a shortcut for importing multiple classes as you were having just a single import statement instead 49
of a separate explicit import statement for each class that you want to import. 50
If you're familiar with SU Well you know that we use select start statement to fetch all the data in 51
that table. 52
So use of stored in this case is analogous to that as we are importing all the classes in a package 53
splatters multiple classes from the same package we need to use either start import or expensive imports 54
. 55
So one question would be Is there any recommendation among the two options one issue it's not important 56
that it can break your record in future. 57
Let's look at an example. 58
Here we're doing a star import on book jot or tittle and our school packages. 59
And we're also using a date us nobody out there to tell Angella that the school have their day class 60
. 61
But for the sake of this discussion let's just assume that when this was written only to tell her that 62
that class so the class that we are seeing here would come from Jowler that you didn't package we can 63
assume that this court also uses some other classes from Jawa not a secure package. 64
And that is for the sake of just it's port not it's after a year or so it did. 65
Classes introduced in dowered obscure package. 66
Let's say it was introduced in a newer version of July IPA Nova that when we re compiled this could 67
we get a compiler at all places where data is used that's because compiler is confused as to which declasse 68
to use that is whether it has to use the Washington Gelada do art of motion and Jawa not a screen but 69
using explicit import for the did class in general that Rudel package how can you go into this situation 70
. 71
That it's adding that the SBA wouldn't have had any effect at all. 72
But this is most likely a pretty corner Nargis that at one stage of explicit import is that it adds 73
more clarity that is just by looking at the import statements. 74
One can tell what courses are being used in that class but start imports. 75
This will be a very tedious task as you would have to skandha and darkness although there is no strict 76
consensus most people seem to prefer explicit imports for the above two reasons. 77
Personally I also prefer explicit inboards as I mentioned earlier an alternate doable using an import 78
statement is to use a fully qualified name of the class that is you would have to do this to create 79
an artist or object. 80
So the fully qualified name includes the package name and we are using it on either side of the assignment 81
. 82
And that's just plain cumbersome. 83
So it is obvious that we should use an import statement however there can be scenarios where you would 84
be forced to use a fully qualified name. 85
For example let's say we want to use both jot or tittle Dogget that I enjoy or killed or dead in the 86
same class. 87
This is a fairly uncommon scenario. 88
But in this case you would have to use a fully qualified name for at least one of the classes there 89
are solutions for this case. 90
And if you're both of them in fossilisation you can use only one explicit Imber. 91
And here is an example we are explicitly importing. 92
You do. 93
So when we did class by simple name as in the first statement here then it would reference Jawa taught 94
you to dog. 95
The second statement is using a fully qualified name no fee I start in part on job at a school then 96
the first statement would still reference dollar that you did. 97
So basically using a simple name of the class will always reference the class from the explicit import 98
so explicit import ex-president's or start import in such a scenario. 99
Just note that in this example we have only two classes but even if there were say mandate classes and 100
10 different packages and we want to use all of them in the same class then we can only explicitly import 101
one of them and for the rest mind you don't have to use them using their fully qualified names. 102
In the second solution we will use fully qualified names for broader classes. 103
Also I start important Jowler daughter tell Angela that as good as it will not make any difference we 104
still have to use fully qualified names as the compiler would simply be confused if he is simple names 105
for the day class. 106
Not that we cannot do this it will give us a compiler it makes sense because if this was allowed you 107
would have to still use fully qualified names wherever data is used. 108
So the only option is to use one of the two solutions we just discussed. 109
That is just at least one explicit import or use only fully qualified means next is a question on whether 110
there are any side effects in using import statements. 111
An answer is No. 112
You can have as many important statements as you want and you may never even use those classes and it 113
will not have any ethnic import statement doesn't make your classes bigger. 114
That is it is not going to import the byte code of the import at us so that's not going to happen. 115
Similarly an import statement does not affect that runtime performance do it simply saves you from typing 116
the fully qualified name of the class. 117
However on your compiler core the compiler replaces the simple class names with the corresponding fully 118
qualified names just not a dollar or two line package which includes classes fundamental to Java is 119
imported by default. 120
So you don't have to explicitly import any of the classes in this package. 121
You can directly access them finally let's do some of the stuff we just learned. 122
No new methods or ideas for this demo. 123
We just try all different scenarios and that would be good. 124
So let's get started with demo OK here we are in the basics class. 125
Let's create a variable called debt off beat. 126
So this is from the job interview deal package and if you compile it says cannot find the similar date 127
so the compiler is unable to define this class called did that because we did not import declasse. 128
So let's put an import statement here that said jobs are not so you are telling the compiler to import 129
the class from the job or to do package 130
but it still gives a compiler. 131
And that's because this statement has been please derk wrong position. 132
So right now it is within the definition of class but it needs to be on top of the class definition 133
or here cannot be inside or after class definition. 134
Get more so that's an import statement. 135
Now let's try starting point. 136
That will come into play if we want to import multiple classes from the same package. 137
So let's just go ahead and 138
import our A-list from the same job or you do package and this would also give a competitor because 139
idlest has to be imported. 140
So let's go scroll up and make this start in quarter and we are good now game so no matter art the second 141
approach me said was how explicit import for both of them 142
a good read that also and this is the most preferred approach for porn most of the developers so it's 143
preferred over starting work. 144
OK. 145
So that's about import statements and that's the first way to use classes to access classes from differ 146
from other packages. 147
And we said the second way was to use fully qualified class needs. 148
So let's try that also. 149
So let me just comment that out and here let me create a new object of the class so to use fully qualified 150
name that's it. 151
Now combine this so it just cannot find symbole dude. 152
So that's because of the fully qualified name has been used only on one side of the assignment or not 153
on the other side. 154
OK so it shows the error on the left side or here. 155
So we need to use on either side. 156
A Navi or good so that's the second approach. 157
But we should use the first approach. 158
The very are using an important element so that we can directly use we can use just a simple name of 159
the class. 160
But here if not we will have to use the entire package name on that's just plain cumbersome and we have 161
to use every where where the classes used. 162
So that's cumbersome. 163
So that's. 164
So we shouldn't be using fully qualified names. 165
But we said that we will have to use fully qualified names in certain scenarios and the scenario was 166
that if you want to use a class with the same name for multiple packages then you don't have a choice 167
but to use at least one fully qualified class name. 168
OK so let's just try out that example too. 169
So let's make this back to date 170
and if you remember the second one was from Joffe-Block I got a school does it's called or test date 171
too. 172
So we want to use that date class from both the packages. 173
So one of them is a fully qualified name and here for the other one we are using an exploit an explicit 174
import. 175
So there is one explicit import on one fully qualified class name students compile this Henri are gone 176
. 177
So you said this is one of the approaches. 178
Right. 179
So one of them is an explicit import. 180
So this particular bit is referencing is referencing the date and the arguably package. 181
And this is unexpressed important. 182
So that's one of the solutions and the second one is to use fully qualified names for both of them so 183
we can just read more. 184
Gus 185
we are good. 186
OK. 187
And the only thing we cannot do is 188
me said We cannot how you can do this we cannot import. 189
But it did cost us explicitly 190
you see. 191
I notice a type with the same simple name is already defined by the single pipe import of data. 192
So the error is here in the second in the second line of type with the same simple name is already defined 193
by a single type import which means that you are already importing a class with the same name. 194
So you can do it again because when we do this we are making a deliberate statement that somewhere in 195
the class that the class will be used. 196
OK. 197
So the compiler does not allow that. 198
Even if you don't use these classes anywhere and if you just how only the importunate mess you still 199
will get that error. 200
So we just call him or not it out and we still get the same error. 201
So you can do that. 202
So that's about it. 203
That's smart. 204
That's all we saw in this lesson. 205
So you can we both explicitly import On-Star imports and we need them to access classes from some packages 206
. 207
And we also say that there are no side effects of having this import statements. 208
So that's one thing we also search. 209
So it's not going to having these statements will not make your classified larger. 210
So we discuss that. 211
So that's important. 212
That's a broader important statement. 213
And thank you and happy courting