Hire everyone in this tutorial I'm going to talk about class variables and class methods.
OK.
So first of all we need to create our Java project.
So in order to create a Java project we need to click on File Neo and then click on Java project.
So for the project name just type in here class variables and class methods.
OK.
And click on finish.
So as you can see here our class is now created.
And then if we need our main class for our project in order to actually execute our Java code.
OK.
So just click on this as our See folder and then click on File New and then click on class.
So for the class name just type in here Main.
And for the package we need a reverse domain here.
So just typing here com that your name dot product name class variables
and class methods.
OK.
And then for the modifiers to the public.
OK.
The access modifier of our class is public.
And here make sure that you check these checkbox here public static void main method.
OK.
And then click on finish.
So as you can see here our main class is now created.
So now let me actually delete this comment here.
OK.
Now I want to create bad objects.
OK.
So first of all you need a class called the bird.
So in order to create a class just click on this package here and then click on File New and then click
on a class.
So for the class name just type in here.
OK.
And for the modifier this is public.
And then click on finish so as you can see here our class called Baird is now created.
So now inside of this class I want to put some instructions in order to show me how to create birth
objects.
So first of all I want to create an instance variable for my birth objects so the data type of my instance
variable is it and the name of my instance variable is speed.
And I want to assign a value to my body.
For example 100 than 80.
OK.
And remember that I told you in the previous tutorial that you should always use this private access
modifier for your instance variables in order not to allow any other classes other than disparate class
to access this spit instance variable directory.
OK so now I want to create a getter my friend and I set her my faith for my speed.
Instance variable.
So in order to create or get her in my head I just need to type in here public.
OK.
The access modifier of my get her method is public.
OK.
And the return type of mine if it is in it and the name of my method is get speed.
And then I need to put in a pair of round brackets after my method name.
And then I need to open and close my method by using these curly braces.
OK.
So inside of this nymphet I just need to return the value of my speed instance value of it.
So here I just typed in return speed.
OK.
So now I want to actually create or set her my fed for my speed instance variable.
So again the access modifier of my method is public and the return type of my Meffert is void and the
name of my method is set speed.
And then I need to put in a pair of round brackets after my Meffert name and this time I want to specify
that my method is going to accept an argument.
So the data type of my argument is int and the name of my argument is value.
And then I need to open and close my method by using these curly braces.
So here I want to actually assign the value of my argument to my speed instance variable.
So first I need to refer to the name of my speed instance variable.
And then I put an assignment operator here and then I refer to the name of my argument.
OK so now I might get her method and my certain method for this instance variable is created.
So now I want to actually create a behavior for my birth objects.
So again the access modifier of my method is public.
So for the behaviors of your objects always use the public access modifier.
OK.
Or you can actually use the package access modifier if you want to only allow the classes inside of
this package to use that behavior.
OK.
So in order to actually use the package access modifier you don't need to write any key word here.
OK.
The package access modifier is the default access modifier for all of your variables and methods.
So the return type of My method is void.
And for example my bird can fly.
OK so the name of my method is fly and then I need to put in a pair of round brackets after my name
and then I need to open and close my fit by using these curly braces and inside of this if I can't put
my statements.
So here I just need to type in system that's out that insulin.
And then inside the prentices I just put it into double quotes here and then inside of these double
quotes I can just type in fly.
And then I need to put in a semi-colon at the end of my statement.
So as you can see here our blueprint is now ready.
So now we want to create weird objects from disparate class.
OK.
So inside of this main class here I just need to double click on this means that we have here in order
to make this a little earlier or much wider.
So now we want to create objects from this birth class.
So first of all I need to refer to the name of my class bird and the name of my object is my beard and
that I put an assignment operator here.
And then I use this key word here new.
And then again you refer to the name of my class and then I put in a pair of round brackets.
After my class name.
OK so now I can actually access the characteristics or behaviors of my birth object.
OK.
So for example I want to access the value of the speed instance variable and then outputs that value
to the console.
So I just to type in here system that out that insulin and it inside of the prentices.
First I need to refer to the name of my object my bird that gets speed OK.
And then I need to put in a semicolon at the end of my statement.
So now if I run my project as you can see this is the value of the speed instance variable.
OK.
Or I can actually tell my birth object to fly my birth dot fly.
OK.
And now if I run my project as you can see this value is outputted to the console.
OK so now I going to actually talk about class variables and class methods.
Class variables are variables defined and stored in the class itself.
Their values apply to the class and all its instances with instance variables.
Each new instance of the class gets a new copy of the instance variables that the class defines each
instance then can change the values of those instance variables without affecting any other instances.
OK I actually showed you some of those examples in the previous tutorials.
I told you that every object actually gets a new copy of the instance valuable that we define in that
specific class.
OK.
So for example if I change the value of the speed instance valuable for this my birth object we are
actually changing a copy of that instance variable.
OK.
And if I create another object from this class the value of that instance variable is going to be unchanged.
OK.
Because we are only creating a copy for our object.
For every object that we create from this class.
Ok this is for instance variables but with class variables is only one copy of that variable exists.
When the class is loaded changing the value of that variable changes it for all instances of that class
redefined class variables by including the static keyword before the value itself.
OK so now let me actually show you some real examples in order to make this concept more meaningful
for you.
So first let me actually comment out these lines of code here.
OK.
And now inside the bird's class here and now inside the parent class here.
Firstly actually change this private access modifier to public.
OK.
And then we need to put this tatic vote here after our access modifier in order to make this instance
valuable a class variable.
OK.
Static.