So now if I actually want to output the value of this instance variable to the console Let's see what
happens system that out that princeling.
And then inside the prentices I defended the name of my object that speaks OK and then I put a semicolon
at the end of my statement.
So now if I run on my project as you can see this is the value of this instance variables of this might
object.
OK first the value was 70.
And then in line 11 we decided to change that value to number 25.
And then in line 12 we decided to out food that's value to the consumer.
So this is our value.
OK.
But this is not the proper way in order to assign a value to our instance variables.
And this is not good.
OK.
We should actually use a method called setter in order to actually assign values to our instance variables.
OK in that way our codes are more secure.
OK so now let me show you how you can do that.
So inside that class we need to create some if it's OK.
So first of all I want to create a get out of my fit and a set in my head for my speed instance variable.
OK.
After creating our spit instance variable i just need to type pain into the return type of My method
is it and the name of my method is get speed.
OK.
And then I put a pair of round brackets after my Meffert name.
And then I open and close my method by using these curly braces.
OK.
So inside this method I can actually return the value of this speed instance variable.
OK.
So I just need to type in here return speed.
OK.
So as you can see here this is our getter method for this instance variable.
The return type of this method is it.
OK.
And the name of this method is get speed.
OK.
And this left has no arguments.
OK.
And inside the Smurfette I am actually returning the value of this speed instance variable return speed.
OK.
And then I can actually create another Meffert which is the setter.
OK.
So here I can just type in void the return type of my Mefford is void.
This never is going to return nothing.
OK.
And the name of this method is set speed.
OK.
And then inside Apprentice's all you want to specify that this method is going to actually accept an
argument.
OK so the data type of my argument is int OK and the name of my argument is for example value and then
I open and close my Meffert by using these curly braces and inside the Smurfette I can actually refer
to the name of my speed instance variable.
OK.
And then I put an assignment operator here.
And then I refer to the value variable here.
OK.
So as you can see here I created or get hurt my fat.
OK this is our Gitomer for.
OK.
I created a get her my friend and set her Mefford for our speed instance variable.
OK.
So these two methods are get here and so term effects of our speed instance variable.
OK.
So this get home if it is going to have a return value of type it.
OK.
And as you can see here I specified that the return type of this get and if it is it.
And inside the Smurfette I returned the value of this instance variable.
I actually created this variable and this variable inside the class.
OK.
So because I actually created these variables inside this class and not inside any of these methods.
OK I can access these variables from any place in my class.
OK but if I for example create a variable inside these settlements for example I can actually create
a variable in a custom variable for example.
This is of value but I cannot access this variable from this method.
OK custom variable.
And if I for example want to assign another value to this variable I cannot do that.
Why.
Because this variable is created inside the Smurfette.
OK.
And we cannot actually refer to this valuable insight.
This run of it these kinds of variables are local variables.
OK this argument here is also of local variable.
OK.
Because we can only refer to these variables inside this method.
OK.
We cannot actually refer to these variables inside a.m.-I.
Ok for example this run method.
OK.
But we can actually access this split instance variable and and this power instance variable from any
places inside our class.
OK.
So as you can see here I can actually access the value of the power instance variable inside the sleep
method.
Ok so I can just refer to the name of my power instance variable and then I can assign another value
to this variable.
So as you can see here we are allowed to do that.
OK.
But here we cannot access this variable that is created inside this set speed method.
OK.
So these kinds of variables are local variables because they are local.
OK.
So hopefully that makes sense.
So now let me actually delete these lines of code here because I want to actually explain the concept
of local variables.
OK.
And as you can see here this is our set friend.
OK.
The return type of this method is void and the name of this method is set speed and this Clifford accepts
an argument.
OK.
And then inside this method we are actually assigning this argument to our sweet instance variable.
OK.
So whenever that we actually call this term effect in our code we need to actually pass our value to
the Smurfette.
OK.
And that insight is nymphet we are actually assigning the past value to our speed instance variable.
Ok so now I want to actually create a set here and I get home I friend for our power instance variable.
So first I'm going to actually create or get hurt my friend.
So the return type of My method is in the name of my method is get power ok and I put a pair of round
brackets after my Meffert name and then I open and close my head by using an opening curly brace followed
by our closing curly brace.
OK.
And then inside this my faith I need to return the value of the power instance variable.
So I just need to type in return power.
OK.
And then let me actually create a set for my faith for our power instance variable.
So the return type of Our method is Voyt and the name of our Meffert is set power.
OK.
And then I put in a pair of round brackets here after my name.
And then I want to specify that this method is going to actually accept arguments.
OK so the data type of my argument is int and the name of my argument is value.
OK.
And then I need to open and close my method by using an opening curly brace followed by our closing
Colibri.
OK.
So inside this method first I need to look first to the name of my power variable power.
OK.
And then I put in an assignment operator here and then I referred to the name of my argument value.
OK.
So whenever that we actually call this Smurfette we need to actually pass our value to the Smurfette
and then that value is going to be assigned to this power instance variable.
Ok so in this way we can actually set values to our power instance variable.
So you may wonder is that how we can actually use these setters and getters methods.
So inside the main class as you can see here.
First let me actually comment out these lines of code here and then we going to create another object
from the start class.
OK.
So first I need to refer to the name of my class stock and then I need to give my object a name.
My second dog.
OK.
And then I put in an assignment operator here and then I use this keyboard here knew.
And then again I referred to the name of the class dog.
And then I put in our programs brackets after my class name.
OK.
So I need to put in a semicolon at the end of my statement.
So as you can see here we created another object from this class.
OK.
So here I want to actually access the value of the instance variables of my second dog object.
OK.
So in the past we actually accessed those instance variables directly.
OK.
As you can see here but here are you want to actually access the value of these instance variables by
using these getters and setters methods.
OK.