welcome back to a nother java tutorial where we've got a good one for
yeah we're going to be gone over classes and objects now java being an
object oriented programming language classes and objects are really the
essence of java they're what make java job
other so this video is going to serve as an introduction but we are going to
cover all the basics to get you on your way if you're already familiar with
java this will still be a good refresher i do want to mention that this video
is part of a larger java tutorial playlist that i have a link that down of the
comments so if you want to see more job or tutorials go check that out so
if you have done any java development
you've actually been using classes as whole time and you might have not
have unknown it if we take a look here every job program has this public
class mean so you've actually been writing all your code inside of a class
so you have be asking what is a class while a class serves as a blueprint
to create objects and objects are an instance of a
class
if that's confusing i don't blame you i think the best way to understand this
is by doing an example and explaining it as i go so let's take a look at
some good or it so here we have the general form of a class you're going
to have the key word class saying hey this is going to be a class that i
wanted to find and then you're going to have to give your class and name
and then the body of the class
last would live in between these two curly braces now there are two
things that class can have either variables or methods so here we define
two variables which when they're in a class they're going to be called
instance variables now these can be really any variable type and you can
have as many as you want so the other thing you can have our methods
so here we are
i've an example of two methods and you can have zero or more in a class
so they can be different return types they can have zero or more
parameters that cetera wanting to mention is that even though it's not
enforced or anything by the language
, a class should be defined as a single entity for example you could have a
class called person and it could have something like a name or and age
but it wouldn't make sense to put something like the weather or
something in that class that's a good way to write messy code and you
don't want to do that so if we go back to our method year let's go ahead
and create a
actual class so i'm using intel ajay i'm gonna go to file new java class and
would want to do here list let's do something little fun here let's let's make
a pokey mom class
or it's so here we see public class you on and and for this tutorial you
don't need to worry about what this public is doing but this is an empty
poke him on class so let's write our instance variables here let's let's give
our poke him on a name
and let's give it a level
and what kind of method should we give this let's write a method that
doesn't return thing called attack
and in here we're just going to print out
bull say the name and then we'll just say attack
now that we have this class to find this class will be a blueprint for trading
objects so how do we declare and initialize objects well first you wanted
just type the class name then you want to give your object and name and
then you want to actually create the object itself which uses the new
keyword and the class name
i'm so an arcades how would that look so we type pokemon
which is the class name and we had to give our object and name will just
caught peel on equals new pokey mine
so for now don't worry about what this parentheses mean we're going to
get into that so assignment in java works from right to left so what's
happening here is first we're creating this object here so java is going into
the memory and it's allocating space to create this object then it's setting
an equal to this pokey mon variable so now if we ever want to access
anything in our pokemon object we use this p one to how do we do that
yeah we're going to be gone over classes and objects now java being an
object oriented programming language classes and objects are really the
essence of java they're what make java job
other so this video is going to serve as an introduction but we are going to
cover all the basics to get you on your way if you're already familiar with
java this will still be a good refresher i do want to mention that this video
is part of a larger java tutorial playlist that i have a link that down of the
comments so if you want to see more job or tutorials go check that out so
if you have done any java development
you've actually been using classes as whole time and you might have not
have unknown it if we take a look here every job program has this public
class mean so you've actually been writing all your code inside of a class
so you have be asking what is a class while a class serves as a blueprint
to create objects and objects are an instance of a
class
if that's confusing i don't blame you i think the best way to understand this
is by doing an example and explaining it as i go so let's take a look at
some good or it so here we have the general form of a class you're going
to have the key word class saying hey this is going to be a class that i
wanted to find and then you're going to have to give your class and name
and then the body of the class
last would live in between these two curly braces now there are two
things that class can have either variables or methods so here we define
two variables which when they're in a class they're going to be called
instance variables now these can be really any variable type and you can
have as many as you want so the other thing you can have our methods
so here we are
i've an example of two methods and you can have zero or more in a class
so they can be different return types they can have zero or more
parameters that cetera wanting to mention is that even though it's not
enforced or anything by the language
, a class should be defined as a single entity for example you could have a
class called person and it could have something like a name or and age
but it wouldn't make sense to put something like the weather or
something in that class that's a good way to write messy code and you
don't want to do that so if we go back to our method year let's go ahead
and create a
actual class so i'm using intel ajay i'm gonna go to file new java class and
would want to do here list let's do something little fun here let's let's make
a pokey mom class
or it's so here we see public class you on and and for this tutorial you
don't need to worry about what this public is doing but this is an empty
poke him on class so let's write our instance variables here let's let's give
our poke him on a name
and let's give it a level
and what kind of method should we give this let's write a method that
doesn't return thing called attack
and in here we're just going to print out
bull say the name and then we'll just say attack
now that we have this class to find this class will be a blueprint for trading
objects so how do we declare and initialize objects well first you wanted
just type the class name then you want to give your object and name and
then you want to actually create the object itself which uses the new
keyword and the class name
i'm so an arcades how would that look so we type pokemon
which is the class name and we had to give our object and name will just
caught peel on equals new pokey mine
so for now don't worry about what this parentheses mean we're going to
get into that so assignment in java works from right to left so what's
happening here is first we're creating this object here so java is going into
the memory and it's allocating space to create this object then it's setting
an equal to this pokey mon variable so now if we ever want to access
anything in our pokemon object we use this p one to how do we do that