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