Hello, Jim.
So they were going to run with you with the glasses.
We'll talk about what glass can contain on good examples.
We learn what our fields and utilization logs, constructors methods and nested classes.
I'm going to explain to you what getters and setters are and how we can use them.
We are going to create our own class and we'll create object of our custom class together.
Let's start from the previous lesson.
You already know that class is our template for objects, but what does it consist of?
Glass can contain constructors, fields, methods and utilization blocks and nested classes to understand
and better.
Let's jump to example.
Let's imagine we are implementing online store and we need to implement our virtual card for shopping.
You already know OPIS and you know that everything is an object.
Take into account we expect multiple users on our website.
We are going to have a lot of cards.
We need to have a template for card object.
That's why we need to create card class.
Here's card class.
I created it beforehand to save time during this video lesson.
I will walk you through it line by line.
Our goal now to understand what class can contain.
First of all, class can contain fields.
Often fields are called properties.
With the help of the fields we can express state of specific object fields can have different modifiers.
For example, fields can be static and non static, final and non final.
We are going to have separate lesson about static keyword and static elements in Java and we'll discuss
final keywords.
Also in separate lesson, this lesson only about classes.
So this is all fields which we have in our class.
Class also can contain initialization blocks.
We may have static and on static initialization blocks.
Why do we need them?
For example, I have some fields which I want to be initialized with values by default.
In this case, every timecards object will be created.
Card counter will be incremented in case cards will be created without user i.e. I know that I need
to sign this card to some user.
Let it be special user with ID one and one of the properties has tax time.
This is also custom type described by us.
I will show it to you later in this lesson.
But now imagine that you have this field and you don't want to leave it, not initialized.
What default value has all reference types of data?
No.
And according to logic that I implemented, I know that tax should not be known to avoid no pointer
exception because I use this variable in court below and b what stands for null pointer exception,
one of the most widespread exception which happens when you call method or new value.
That's why it's called no pointer exception.
We'll learn more about exceptions in separate section.
For now, I want at least some default value should be assigned to this variable.
That is exactly what I'm doing here.
I using this civilization look to initialize this field, the similar situation with our discount object.
I initialize this field to make sure it won't be no talking about starting blocks, it is worth to mention
that they are very narrow application domain Whiskas class example.
It was hard for me to come up with the example of static initialization block.
This block will be executed only once one class will be uploaded into GBM.
In this case we just write text to console.
Good example of static Blokland civilization usage is driver classis.
You always need to register driver in the system so it would be comfortable to use static initialization
work for this.
For example, once driver class is uploaded into GBM, we can register it in some services.
This is a real life example which we will see later in this course.
But now you at least understand how potentially we can use static in civilization.
Bloks problems it again.
Universalization looks let's move on may contain constructor's constructor is a special method which
should be called to instantiate object.
Important things to mention about the constructor constructor name should always be the same as class
name.
If your class name is called, then constructor is also called rule about writing.
Method starting from lowercase is not applied to constructors.
It doesn't have return type.
You remember that all method should have return type in constructor.
You should not specify return time because constructors always by default return reference to the object
and keep memory.
You always have constructor by default.
Let me show you this.
Even if I wouldn't have this constructors, I would still have default one.
Let me comment on this court and now let's jump to the demo file and create card object.
Let me declare a variable of type card and assign cards.
Object to it.
The great object I write new keyword.
This is a common to Java to allocate memory for object in here and after that I call constructor and
print cards object to cancel.
Let's run this program.
And here we are, you see that quote from Start, a conservation blog executed first, and here's our
character object printed to console.
Now let me create custom constructor.
Let's pretend I want to create card with specified cartridges and user aguy.
And when I created custom constructor, there is no default constructor anymore.
And here is see compilation error.
The constructor card is undefined.
That's why if you want to declare a custom constructor and keep constructor by default, you should
specified explicitly when I am Corman's, this constructor compilation error is gone.
Let's take a look how we can use custom constructor.
We know that our constructor can take two integers as arguments and you see that the first parameter
will be assigned to a property of this object in the second parameter will be assigned to user the property
of the current object.
You can see that I use this keyword to reference current object because this idea, if this property
in card object and ID is just a local variable of the constructor, that that makes sense.
So inside the class you have keywords which always reference to the specific object.
Now you can call your custom constructor like this new card and parse constructor arguments when we
bring second car token.
So you can see that IJI and user idea of this class is different from the first class object.
Now, when you know what the constructor's, you might be wondering why do we need initialization blocks
if they have constructor's on this example, you can see that no matter which constructor will be called
utilisation, LOC will be executed.
In both cases.
Knowing this, you can use initialization blocks to put their code, which you want to execute no matter
what constructor will be called.
Is it clear now what else our class can have?
Our class can have methods.
That is behavior of our object.
For example, our cars can add new product to the cart.
Let's not get deep dive into the logic here.
I will share with you source code of this lesson and you will be able to take a look miserably at implementation.
But now let's understand that methods are behavior of our object and can change state of our object.
And now you can understand the difference between method and function.
Function can exist without object independently.
Method is the behavior of the object.
That's why in Olby we call specific box of code, which can be invoked by their names methods but not
functions methods.
This is something that is always associated with an object.
In this particular example, our card contains an array of products and we can add product to this card,
namely to this array of products.
That is exactly what this method does.
It adds new product and change the state of our cart.
Notice that each time new product is added to the cart, other methods are called to calculate total
net price and total gross price.
These are two private methods.
That means nobody outside this class can call these methods.
I can call these methods from this class.
Only X modifiers will be covered in detail in encapsulation.
Lesson in your code method and location might look like this.
You created car to object and after that use its behavior to add product to the cart products is also
our custom class.
By the way, here it is, just two properties for the sake of our example.
And he seemed concerned that after we added two products, total net price, total gross price and total
tax have been changed.
After these methods, I have other methods that are called getters and setters, why do we need them?
You see that all properties of cart objects are private.
That is because I want to have full control over how people can interact with my object and how they
can change.
State of object was usually used to rid properties of objects.
You can be 100 if we are making our properties private so nobody could access them and change them,
then why do we need to create better sensors?
Let me explain you on this example.
You can create together and return your products array, but this means that everybody who has links
to your products array can change state of disarray.
Just using this reference, for example, having reference to products, every object I can add to more
products in it and state of the card will be different.
In this case, there would be two more products in CART, but now when you can write code of your car,
you can say that you want to give reference to this array.
Everyone who wants to investigate state of products are we can do this and you can return a copy of
the script by doing this.
You can be sure that even if in some other places reference to products that will be obtained, they
will get reference to the copy of your and won't be able to change state of our regional products away
from your car.
This is critical for us because based on the products from this array, we calculate total net and total
gross price.
Imagine that somebody would take the reference to products are a few more products to it without recalculate
total net and gross price and would just proceed to checkout with this car.
Customers would be happy that they got more items for free, but our management definitely won't be
happy.
And if I don't want that somebody said a completely new array of products in my car, I just don't create
Setar.
That's it.
But if you have Setar, you can verify arguments to make sure that they are valid.
According to our business logic, for example, everyone can, said Cartledge here, but nobody can
set a value less than zero because I wrote the code to verify this in case I'd is less than zero.
Just exit from this method and you can see that Rejon keyword is also used to exit method in both methods.
Even when you don't have intention to return some value from method or like here, for example, anybody
can add product to the cart.
But in case somebody is trying to add no into my array, I don't allow to do this and exit from method.
Remember that validation of all arguments which were passed to the method if the rule which you always
must follow.
By the way, if you want easily generated getters and setters, use next hotkey old shift s and select
generate getters and setters.
You can select fields for which you need getters and setters or only getters.
For example, if you don't want the grade setters and vice versa, press generate to generate code.
I already have all necessary getters and setters.
That is why I wouldn't press this button.
Now, in case you forgot hotkeys press mouse right.
Click select source and then generate getters and setters.
We have to string method here, we are going to talk about this method in details in the lesson about
object class.
But in the meantime, let me explain you what this method is about in a few words in Arkansas here we
see text representation of our the object.
That is because to string method is overridden here.
And you can see how the string is created from combination of fields from our car object when we pass
reference type to Winterland, method to string method call is invisible for us and when to string method
is called this code is executed and this string is returned.
That's why we see rideable representation of our card object to generate to string method use the same
hotkey as we discussed earlier, all shifts and select generate to string.
It says that I already have two string but still I want to show you details you can select which fields
methods you would like to use to create string which will be returned after the string method call.
Besides what we have already discussed, glasses can also contain nested glasses, nested glasses can
be static and non static, static nested glasses, usually called just nested static glasses and nested,
not static glasses, usually called inner glasses.
And here you see discount untax glasses we use for both of these types in our cars glass.
And I believe you remember the beginning reinitialize variables of these types with default values in
our initialization lock to not overwhelm you with information.
In this lesson, let's agree to learn types of classes in the separate lesson.
For now, let's repeat one more time.
One glass can contain glass, can contain fildes, initialization blocks, constructors methods, nested
classes.
And we know that nested classes could be nested static classes and in their classes.
I will share with you links to the source code in attachments to this lesson so you can review it one
more time.
Method classes will be discussed in the separate lesson.
And now let's review what we have learned today.
Today we learned what class can contain and how to create our custom objects.
Now you know the difference between a method and a function, really, of the concept of getters and
setters and how to generate them easily in eclipse.
Take a look one more time in the source code of examples which we reviewed during the lesson and be
prepared for the next lesson.
Thank you for your attention and see you in the next lesson.