Tan115's picture
Add files using upload-large-folder tool
407b94e verified
Raw
History Blame Contribute Delete
21.2 kB
1
00:00:05,000 --> 00:00:06,000
Hello, Jim.
2
00:00:06,000 --> 00:00:08,000
So they were going to run with you with the glasses.
3
00:00:09,000 --> 00:00:12,000
We'll talk about what glass can contain on good examples.
4
00:00:12,000 --> 00:00:17,000
We learn what our fields and utilization logs, constructors methods and nested classes.
5
00:00:17,000 --> 00:00:21,000
I'm going to explain to you what getters and setters are and how we can use them.
6
00:00:21,000 --> 00:00:26,000
We are going to create our own class and we'll create object of our custom class together.
7
00:00:26,000 --> 00:00:28,000
Let's start from the previous lesson.
8
00:00:28,000 --> 00:00:33,000
You already know that class is our template for objects, but what does it consist of?
9
00:00:33,000 --> 00:00:41,000
Glass can contain constructors, fields, methods and utilization blocks and nested classes to understand
10
00:00:41,000 --> 00:00:41,000
and better.
11
00:00:41,000 --> 00:00:43,000
Let's jump to example.
12
00:00:43,000 --> 00:00:48,000
Let's imagine we are implementing online store and we need to implement our virtual card for shopping.
13
00:00:49,000 --> 00:00:53,000
You already know OPIS and you know that everything is an object.
14
00:00:53,000 --> 00:00:56,000
Take into account we expect multiple users on our website.
15
00:00:56,000 --> 00:00:58,000
We are going to have a lot of cards.
16
00:00:58,000 --> 00:01:01,000
We need to have a template for card object.
17
00:01:01,000 --> 00:01:04,000
That's why we need to create card class.
18
00:01:04,000 --> 00:01:05,000
Here's card class.
19
00:01:05,000 --> 00:01:09,000
I created it beforehand to save time during this video lesson.
20
00:01:09,000 --> 00:01:11,000
I will walk you through it line by line.
21
00:01:12,000 --> 00:01:14,000
Our goal now to understand what class can contain.
22
00:01:15,000 --> 00:01:17,000
First of all, class can contain fields.
23
00:01:18,000 --> 00:01:19,000
Often fields are called properties.
24
00:01:20,000 --> 00:01:25,000
With the help of the fields we can express state of specific object fields can have different modifiers.
25
00:01:26,000 --> 00:01:30,000
For example, fields can be static and non static, final and non final.
26
00:01:30,000 --> 00:01:35,000
We are going to have separate lesson about static keyword and static elements in Java and we'll discuss
27
00:01:35,000 --> 00:01:36,000
final keywords.
28
00:01:36,000 --> 00:01:40,000
Also in separate lesson, this lesson only about classes.
29
00:01:40,000 --> 00:01:43,000
So this is all fields which we have in our class.
30
00:01:43,000 --> 00:01:45,000
Class also can contain initialization blocks.
31
00:01:46,000 --> 00:01:49,000
We may have static and on static initialization blocks.
32
00:01:49,000 --> 00:01:50,000
Why do we need them?
33
00:01:50,000 --> 00:01:55,000
For example, I have some fields which I want to be initialized with values by default.
34
00:01:56,000 --> 00:01:59,000
In this case, every timecards object will be created.
35
00:01:59,000 --> 00:02:05,000
Card counter will be incremented in case cards will be created without user i.e. I know that I need
36
00:02:05,000 --> 00:02:07,000
to sign this card to some user.
37
00:02:08,000 --> 00:02:13,000
Let it be special user with ID one and one of the properties has tax time.
38
00:02:13,000 --> 00:02:16,000
This is also custom type described by us.
39
00:02:16,000 --> 00:02:18,000
I will show it to you later in this lesson.
40
00:02:19,000 --> 00:02:24,000
But now imagine that you have this field and you don't want to leave it, not initialized.
41
00:02:24,000 --> 00:02:27,000
What default value has all reference types of data?
42
00:02:27,000 --> 00:02:28,000
No.
43
00:02:28,000 --> 00:02:34,000
And according to logic that I implemented, I know that tax should not be known to avoid no pointer
44
00:02:34,000 --> 00:02:40,000
exception because I use this variable in court below and b what stands for null pointer exception,
45
00:02:40,000 --> 00:02:44,000
one of the most widespread exception which happens when you call method or new value.
46
00:02:45,000 --> 00:02:47,000
That's why it's called no pointer exception.
47
00:02:48,000 --> 00:02:50,000
We'll learn more about exceptions in separate section.
48
00:02:50,000 --> 00:02:54,000
For now, I want at least some default value should be assigned to this variable.
49
00:02:55,000 --> 00:02:57,000
That is exactly what I'm doing here.
50
00:02:57,000 --> 00:03:02,000
I using this civilization look to initialize this field, the similar situation with our discount object.
51
00:03:02,000 --> 00:03:09,000
I initialize this field to make sure it won't be no talking about starting blocks, it is worth to mention
52
00:03:09,000 --> 00:03:13,000
that they are very narrow application domain Whiskas class example.
53
00:03:13,000 --> 00:03:16,000
It was hard for me to come up with the example of static initialization block.
54
00:03:17,000 --> 00:03:22,000
This block will be executed only once one class will be uploaded into GBM.
55
00:03:22,000 --> 00:03:25,000
In this case we just write text to console.
56
00:03:25,000 --> 00:03:29,000
Good example of static Blokland civilization usage is driver classis.
57
00:03:29,000 --> 00:03:35,000
You always need to register driver in the system so it would be comfortable to use static initialization
58
00:03:35,000 --> 00:03:36,000
work for this.
59
00:03:36,000 --> 00:03:41,000
For example, once driver class is uploaded into GBM, we can register it in some services.
60
00:03:42,000 --> 00:03:45,000
This is a real life example which we will see later in this course.
61
00:03:45,000 --> 00:03:50,000
But now you at least understand how potentially we can use static in civilization.
62
00:03:50,000 --> 00:03:52,000
Bloks problems it again.
63
00:03:52,000 --> 00:03:59,000
Universalization looks let's move on may contain constructor's constructor is a special method which
64
00:03:59,000 --> 00:04:01,000
should be called to instantiate object.
65
00:04:01,000 --> 00:04:06,000
Important things to mention about the constructor constructor name should always be the same as class
66
00:04:06,000 --> 00:04:07,000
name.
67
00:04:07,000 --> 00:04:12,000
If your class name is called, then constructor is also called rule about writing.
68
00:04:12,000 --> 00:04:15,000
Method starting from lowercase is not applied to constructors.
69
00:04:15,000 --> 00:04:17,000
It doesn't have return type.
70
00:04:17,000 --> 00:04:21,000
You remember that all method should have return type in constructor.
71
00:04:21,000 --> 00:04:27,000
You should not specify return time because constructors always by default return reference to the object
72
00:04:27,000 --> 00:04:27,000
and keep memory.
73
00:04:28,000 --> 00:04:30,000
You always have constructor by default.
74
00:04:30,000 --> 00:04:31,000
Let me show you this.
75
00:04:32,000 --> 00:04:36,000
Even if I wouldn't have this constructors, I would still have default one.
76
00:04:36,000 --> 00:04:41,000
Let me comment on this court and now let's jump to the demo file and create card object.
77
00:04:42,000 --> 00:04:45,000
Let me declare a variable of type card and assign cards.
78
00:04:45,000 --> 00:04:46,000
Object to it.
79
00:04:46,000 --> 00:04:48,000
The great object I write new keyword.
80
00:04:49,000 --> 00:04:55,000
This is a common to Java to allocate memory for object in here and after that I call constructor and
81
00:04:55,000 --> 00:04:56,000
print cards object to cancel.
82
00:04:57,000 --> 00:04:58,000
Let's run this program.
83
00:04:59,000 --> 00:05:05,000
And here we are, you see that quote from Start, a conservation blog executed first, and here's our
84
00:05:05,000 --> 00:05:06,000
character object printed to console.
85
00:05:07,000 --> 00:05:09,000
Now let me create custom constructor.
86
00:05:09,000 --> 00:05:13,000
Let's pretend I want to create card with specified cartridges and user aguy.
87
00:05:14,000 --> 00:05:18,000
And when I created custom constructor, there is no default constructor anymore.
88
00:05:18,000 --> 00:05:20,000
And here is see compilation error.
89
00:05:20,000 --> 00:05:22,000
The constructor card is undefined.
90
00:05:23,000 --> 00:05:27,000
That's why if you want to declare a custom constructor and keep constructor by default, you should
91
00:05:27,000 --> 00:05:32,000
specified explicitly when I am Corman's, this constructor compilation error is gone.
92
00:05:33,000 --> 00:05:36,000
Let's take a look how we can use custom constructor.
93
00:05:36,000 --> 00:05:42,000
We know that our constructor can take two integers as arguments and you see that the first parameter
94
00:05:42,000 --> 00:05:48,000
will be assigned to a property of this object in the second parameter will be assigned to user the property
95
00:05:48,000 --> 00:05:49,000
of the current object.
96
00:05:50,000 --> 00:05:56,000
You can see that I use this keyword to reference current object because this idea, if this property
97
00:05:56,000 --> 00:06:03,000
in card object and ID is just a local variable of the constructor, that that makes sense.
98
00:06:03,000 --> 00:06:09,000
So inside the class you have keywords which always reference to the specific object.
99
00:06:10,000 --> 00:06:16,000
Now you can call your custom constructor like this new card and parse constructor arguments when we
100
00:06:16,000 --> 00:06:17,000
bring second car token.
101
00:06:17,000 --> 00:06:22,000
So you can see that IJI and user idea of this class is different from the first class object.
102
00:06:23,000 --> 00:06:28,000
Now, when you know what the constructor's, you might be wondering why do we need initialization blocks
103
00:06:28,000 --> 00:06:33,000
if they have constructor's on this example, you can see that no matter which constructor will be called
104
00:06:33,000 --> 00:06:35,000
utilisation, LOC will be executed.
105
00:06:35,000 --> 00:06:36,000
In both cases.
106
00:06:36,000 --> 00:06:42,000
Knowing this, you can use initialization blocks to put their code, which you want to execute no matter
107
00:06:42,000 --> 00:06:43,000
what constructor will be called.
108
00:06:43,000 --> 00:06:47,000
Is it clear now what else our class can have?
109
00:06:47,000 --> 00:06:49,000
Our class can have methods.
110
00:06:49,000 --> 00:06:51,000
That is behavior of our object.
111
00:06:51,000 --> 00:06:54,000
For example, our cars can add new product to the cart.
112
00:06:55,000 --> 00:06:57,000
Let's not get deep dive into the logic here.
113
00:06:57,000 --> 00:07:03,000
I will share with you source code of this lesson and you will be able to take a look miserably at implementation.
114
00:07:03,000 --> 00:07:09,000
But now let's understand that methods are behavior of our object and can change state of our object.
115
00:07:10,000 --> 00:07:13,000
And now you can understand the difference between method and function.
116
00:07:14,000 --> 00:07:17,000
Function can exist without object independently.
117
00:07:17,000 --> 00:07:19,000
Method is the behavior of the object.
118
00:07:20,000 --> 00:07:26,000
That's why in Olby we call specific box of code, which can be invoked by their names methods but not
119
00:07:26,000 --> 00:07:28,000
functions methods.
120
00:07:28,000 --> 00:07:31,000
This is something that is always associated with an object.
121
00:07:31,000 --> 00:07:37,000
In this particular example, our card contains an array of products and we can add product to this card,
122
00:07:37,000 --> 00:07:39,000
namely to this array of products.
123
00:07:40,000 --> 00:07:42,000
That is exactly what this method does.
124
00:07:42,000 --> 00:07:46,000
It adds new product and change the state of our cart.
125
00:07:46,000 --> 00:07:51,000
Notice that each time new product is added to the cart, other methods are called to calculate total
126
00:07:51,000 --> 00:07:53,000
net price and total gross price.
127
00:07:54,000 --> 00:07:55,000
These are two private methods.
128
00:07:56,000 --> 00:08:00,000
That means nobody outside this class can call these methods.
129
00:08:00,000 --> 00:08:02,000
I can call these methods from this class.
130
00:08:02,000 --> 00:08:07,000
Only X modifiers will be covered in detail in encapsulation.
131
00:08:07,000 --> 00:08:10,000
Lesson in your code method and location might look like this.
132
00:08:11,000 --> 00:08:16,000
You created car to object and after that use its behavior to add product to the cart products is also
133
00:08:16,000 --> 00:08:17,000
our custom class.
134
00:08:17,000 --> 00:08:22,000
By the way, here it is, just two properties for the sake of our example.
135
00:08:23,000 --> 00:08:29,000
And he seemed concerned that after we added two products, total net price, total gross price and total
136
00:08:29,000 --> 00:08:30,000
tax have been changed.
137
00:08:31,000 --> 00:08:37,000
After these methods, I have other methods that are called getters and setters, why do we need them?
138
00:08:38,000 --> 00:08:40,000
You see that all properties of cart objects are private.
139
00:08:41,000 --> 00:08:46,000
That is because I want to have full control over how people can interact with my object and how they
140
00:08:46,000 --> 00:08:47,000
can change.
141
00:08:47,000 --> 00:08:51,000
State of object was usually used to rid properties of objects.
142
00:08:51,000 --> 00:08:57,000
You can be 100 if we are making our properties private so nobody could access them and change them,
143
00:08:57,000 --> 00:08:59,000
then why do we need to create better sensors?
144
00:09:00,000 --> 00:09:02,000
Let me explain you on this example.
145
00:09:02,000 --> 00:09:08,000
You can create together and return your products array, but this means that everybody who has links
146
00:09:08,000 --> 00:09:10,000
to your products array can change state of disarray.
147
00:09:10,000 --> 00:09:16,000
Just using this reference, for example, having reference to products, every object I can add to more
148
00:09:16,000 --> 00:09:20,000
products in it and state of the card will be different.
149
00:09:20,000 --> 00:09:26,000
In this case, there would be two more products in CART, but now when you can write code of your car,
150
00:09:26,000 --> 00:09:29,000
you can say that you want to give reference to this array.
151
00:09:30,000 --> 00:09:35,000
Everyone who wants to investigate state of products are we can do this and you can return a copy of
152
00:09:35,000 --> 00:09:37,000
the script by doing this.
153
00:09:37,000 --> 00:09:42,000
You can be sure that even if in some other places reference to products that will be obtained, they
154
00:09:42,000 --> 00:09:48,000
will get reference to the copy of your and won't be able to change state of our regional products away
155
00:09:48,000 --> 00:09:49,000
from your car.
156
00:09:49,000 --> 00:09:54,000
This is critical for us because based on the products from this array, we calculate total net and total
157
00:09:54,000 --> 00:09:55,000
gross price.
158
00:09:56,000 --> 00:10:01,000
Imagine that somebody would take the reference to products are a few more products to it without recalculate
159
00:10:01,000 --> 00:10:05,000
total net and gross price and would just proceed to checkout with this car.
160
00:10:06,000 --> 00:10:12,000
Customers would be happy that they got more items for free, but our management definitely won't be
161
00:10:12,000 --> 00:10:12,000
happy.
162
00:10:13,000 --> 00:10:18,000
And if I don't want that somebody said a completely new array of products in my car, I just don't create
163
00:10:18,000 --> 00:10:19,000
Setar.
164
00:10:19,000 --> 00:10:19,000
That's it.
165
00:10:20,000 --> 00:10:24,000
But if you have Setar, you can verify arguments to make sure that they are valid.
166
00:10:24,000 --> 00:10:30,000
According to our business logic, for example, everyone can, said Cartledge here, but nobody can
167
00:10:30,000 --> 00:10:37,000
set a value less than zero because I wrote the code to verify this in case I'd is less than zero.
168
00:10:37,000 --> 00:10:43,000
Just exit from this method and you can see that Rejon keyword is also used to exit method in both methods.
169
00:10:44,000 --> 00:10:50,000
Even when you don't have intention to return some value from method or like here, for example, anybody
170
00:10:50,000 --> 00:10:51,000
can add product to the cart.
171
00:10:51,000 --> 00:10:57,000
But in case somebody is trying to add no into my array, I don't allow to do this and exit from method.
172
00:10:57,000 --> 00:11:03,000
Remember that validation of all arguments which were passed to the method if the rule which you always
173
00:11:03,000 --> 00:11:04,000
must follow.
174
00:11:04,000 --> 00:11:12,000
By the way, if you want easily generated getters and setters, use next hotkey old shift s and select
175
00:11:12,000 --> 00:11:13,000
generate getters and setters.
176
00:11:14,000 --> 00:11:17,000
You can select fields for which you need getters and setters or only getters.
177
00:11:17,000 --> 00:11:23,000
For example, if you don't want the grade setters and vice versa, press generate to generate code.
178
00:11:23,000 --> 00:11:25,000
I already have all necessary getters and setters.
179
00:11:25,000 --> 00:11:27,000
That is why I wouldn't press this button.
180
00:11:27,000 --> 00:11:30,000
Now, in case you forgot hotkeys press mouse right.
181
00:11:30,000 --> 00:11:33,000
Click select source and then generate getters and setters.
182
00:11:34,000 --> 00:11:40,000
We have to string method here, we are going to talk about this method in details in the lesson about
183
00:11:40,000 --> 00:11:40,000
object class.
184
00:11:41,000 --> 00:11:46,000
But in the meantime, let me explain you what this method is about in a few words in Arkansas here we
185
00:11:46,000 --> 00:11:49,000
see text representation of our the object.
186
00:11:49,000 --> 00:11:52,000
That is because to string method is overridden here.
187
00:11:52,000 --> 00:11:59,000
And you can see how the string is created from combination of fields from our car object when we pass
188
00:11:59,000 --> 00:12:04,000
reference type to Winterland, method to string method call is invisible for us and when to string method
189
00:12:04,000 --> 00:12:08,000
is called this code is executed and this string is returned.
190
00:12:09,000 --> 00:12:15,000
That's why we see rideable representation of our card object to generate to string method use the same
191
00:12:15,000 --> 00:12:19,000
hotkey as we discussed earlier, all shifts and select generate to string.
192
00:12:20,000 --> 00:12:26,000
It says that I already have two string but still I want to show you details you can select which fields
193
00:12:26,000 --> 00:12:30,000
methods you would like to use to create string which will be returned after the string method call.
194
00:12:32,000 --> 00:12:37,000
Besides what we have already discussed, glasses can also contain nested glasses, nested glasses can
195
00:12:37,000 --> 00:12:44,000
be static and non static, static nested glasses, usually called just nested static glasses and nested,
196
00:12:44,000 --> 00:12:47,000
not static glasses, usually called inner glasses.
197
00:12:48,000 --> 00:12:53,000
And here you see discount untax glasses we use for both of these types in our cars glass.
198
00:12:54,000 --> 00:12:59,000
And I believe you remember the beginning reinitialize variables of these types with default values in
199
00:12:59,000 --> 00:13:03,000
our initialization lock to not overwhelm you with information.
200
00:13:03,000 --> 00:13:07,000
In this lesson, let's agree to learn types of classes in the separate lesson.
201
00:13:07,000 --> 00:13:09,000
For now, let's repeat one more time.
202
00:13:09,000 --> 00:13:16,000
One glass can contain glass, can contain fildes, initialization blocks, constructors methods, nested
203
00:13:16,000 --> 00:13:17,000
classes.
204
00:13:17,000 --> 00:13:22,000
And we know that nested classes could be nested static classes and in their classes.
205
00:13:22,000 --> 00:13:27,000
I will share with you links to the source code in attachments to this lesson so you can review it one
206
00:13:27,000 --> 00:13:27,000
more time.
207
00:13:28,000 --> 00:13:30,000
Method classes will be discussed in the separate lesson.
208
00:13:31,000 --> 00:13:33,000
And now let's review what we have learned today.
209
00:13:34,000 --> 00:13:38,000
Today we learned what class can contain and how to create our custom objects.
210
00:13:38,000 --> 00:13:43,000
Now you know the difference between a method and a function, really, of the concept of getters and
211
00:13:43,000 --> 00:13:46,000
setters and how to generate them easily in eclipse.
212
00:13:47,000 --> 00:13:51,000
Take a look one more time in the source code of examples which we reviewed during the lesson and be
213
00:13:51,000 --> 00:13:53,000
prepared for the next lesson.
214
00:13:53,000 --> 00:13:56,000
Thank you for your attention and see you in the next lesson.