Java Classes & Objects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to another java tutorial where we've got a good one for you we're going to be going 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 java 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 i will link that down in the comments so if you want to see more java tutorials go check that out so if you have done any java development you've actually been using classes this whole time and you might have not even known it if we take a look here every java program has this public class main so you've actually been writing all your code inside of a class so you might be asking what is a class well 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 code all right so here we have the general form of a class you're going to have the keyword 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 a name and then the body of the class would live in between these two curly braces now there are two things a 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 are methods so here we have 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 etc one thing 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 an 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 here let's go ahead and create a actual class so i'm using intellij i'm going to go to file new java class and uh what do we want to do here let's let's do something a little fun here let's uh let's make a pokemon class all right so here we see uh public class pokemon and and for this tutorial you don't need to worry about what this public is doing but this is an empty pokemon class so let's write our instance variables here let's uh let's give our pokemon a name and let's give it a level and what kind of method should we give this uh let's write a method that doesn't return anything called attack and in here we're just going to print out we'll say the name and then we'll just say attack now that we have this class defined this class will be a blueprint for creating objects so how do we declare and initialize objects well first you want to just type the class name then you want to give your object a name and then you want to actually create the object itself which uses the new keyword and the class name so in our case how would that look so we type pokemon [Music] which is the class name and we have to give our object a name we'll just call it p1 equals new pokemon so for now uh 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 it equal to this pokemon variable so now if we ever want to access anything in our pokemon object we use this p1 so how do we do that let's take a look so now if we want to set the name and the level for our object we would do something like p1 and then we would use a period here which is called the dot operator so we do dot name and then we give our pokemon the name i feel like for the first one we got to go with pikachu and then we got to give it give it a level so it'd be p1 dot level equals let's just say level 10. let's go ahead and print that out all right so let's go ahead and run that and we see that it says pikachu 10 which is what we expected all right so now we have one object but now you can create as many objects as you want so let's go ahead and create another one let's call this one p2 so let's give this one a name and a level all right so we have another one we named it evie and we gave it a level of 20. and then let's go ahead and call the attack method so now if we run that we see that it says evie attack and here we should actually go and add a space here so if we run that we see that it's now properly formatted and it says ev attack one important thing to note is that these objects have their own set of variables so p1 gets its own name and its own level p2 gets its own name and its own level if we change the variable for p1 it does not affect anything in p2 that's really one of the fundamental principles of objects it's that variables and methods are encapsulated within the object and changing the variable in one object does not affect the variables in another object so that's really the basic idea of classes and objects the class is a blueprint for some kind of edit entity in this case pokemon and then we use that class to create objects of it which are have variables and methods defined of from that class all right the next thing we're going to go over are constructors sometimes when you create an object you want some sort of code to happen maybe you want some variables to get set initially that's where constructors come into play these are special methods that get invoked as soon as an object is created so if we go back to our pokemon class how that's going to look is it's going to be its own method it's not going to have any return type and it's always going to be the same name as the class so in this case it would just be pokemon we have our parentheses and then we have the body here so say initially when we want a pokemon to get created we automatically want every all of them to be set to level one so what we would do is we would just say level equals one so now when an object pokemon is created this function gets called and the level automatically gets set to one let's take a look at an example so let's go ahead and get rid of pretty much all this here and let's go ahead and print out the level so we see that we're going to print out p1.level now if we didn't have any constructor this integer always gets set to zero but since we have that constructor where we set it to one we run the code and we see that a1 gets printed now say we want uh the user to give us a name and a level how would we do that well what we could do is we could add another constructor that has parameters so let's go ahead and do that and then in the body of the method we'll just set our actual variables in the class equal to what gets passed in as the parameters all right so let's go back to our main function our main method now when i say method and function i'm using these interchangeably in java they're called methods in other languages they might be called functions but i'm referring to methods all right now let's go ahead and create an object passing in arguments for our constructor so how that would look like is we would just create an object like we normally would now when we want to pass in variables to our constructor this is when remember when i said we're going to talk about these parentheses later on well this is that time so the arguments would actually get passed in into these parentheses and this is what represents the constructor so let's go ahead and pass in a name and a level so now this first argument refers back to this string p name and this level 25 refers to this intp level and then our name and our level our instance variables get set to these arguments and uh and yeah let's go ahead and print this out all right so initially we're just printing out p1.level and then we're going to call p1.attack let's go ahead and run that so we see that they're printed on the same line let's go ahead and give us a new line here so we see p1.level prints out 25 and then p1 dot attack calls eevee attack so we see that the eevee and the 25 get set because we passed these in to the constructor all right the last thing i want to talk about is the this keyword say for some reason in our constructor we wanted to call this string name and string level and then in here we want to set this name equal to name and level equal to level well you see how this gets kind of weird here because uh name is the same as our instance variable name and same with level so which one does it use in here well what it does is it always uses the most local variable so in this case it would co it would use the uh the arguments that get passed in so what's happening is name is just getting set back to name level is getting set to level none of these instance variables are being used at all so how do we get around this well java has the this keyword so what we do is we would just do this dot name and this dot level so whenever you use the this keyword it's saying use the variables that are actually defined in this class so this is just an explicit way of telling java that we want to use these variables and you can even use this for methods as well so if we wanted for some reason we could actually call this dot attack in the constructor all right that's going to wrap up this tutorial just to recap we talked about creating classes creating an object class of that type we went over constructors to initialize instance variables as well as passing parameters to constructors finally we wrapped it up by talking about the this keyword so we went over a ton in this video we got the basics of classes and objects down but honestly we've still only scratched the surface with classes and objects i plan to do another video kind of a part two to this where we go uh more in depth with what you can really do with classes and objects that are very powerful so yeah that video should be coming up make sure you guys subscribe to uh get a notification when that video gets released make sure you guys leave a like if you did like the video and of course uh if you have any questions leave them down in the comments as i mentioned i do have a java playlist that you can check out with videos that are continuously being added to it but as always thank you guys so much for watching and i'll see you in the next [Music] video [Music] bye
Info
Channel: Keep On Coding
Views: 170,864
Rating: undefined out of 5
Keywords: java classes, java objects, java classes and objects tutorial
Id: IUqKuGNasdM
Channel Id: undefined
Length: 11min 36sec (696 seconds)
Published: Sun Dec 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.