Java constructors 👷

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going everybody it's your bro here hope you're doing well and in this video i'm going to teach you guys all about constructors in java so sit back relax and enjoy the show you too can become a hero and save our channel by punching the like button throw a comment down below and smash the subscribe button alright guys and gals in this video i'm going to be explaining constructors to you all now a constructor is a special method that is called when an object is instantiated and when we instantiate something that's just a fancy term for creating an object now for this example i have two classes i have my main class that contains my main method and i also have another class within my project folder called human we're going to be creating some human objects today now normally the steps of creating an object we would type in the name of the class for this example it's human a unique name for this specific object we'll just call this human a lowercase equals new human now this portion works very similar to a standard method call except a constructor is a special method that works behind the scenes that will create an instance of a class for us it will instantiate an object basically speaking our constructor is acting as a special method and we can even pass in arguments when we instantiate it so if we would like to do something with these arguments like assign them to the attributes of our object we can set up the constructor within the class so this is how to create the constructor within our class it's going to have the same name as the class for this example it's human followed by a set of parentheses and then a set of curly braces and that's it we have our constructor so when we create an instance of this class it's going to call the constructor much like a method and anything within the constructor will be performed just like a standard method so we can actually set up some parameters and pass in some arguments when we create our object so let's set up some parameters for our human class what are a few attributes that a human might have for one a name like bob or something so let's define the first parameter of string name perhaps an age int age and then a weight int actually let's make this a double double weight so we have now set up three parameters for this human class let's head back to our main class now we're going to have a problem that's because we do not have matching arguments to send our human that's because we're saying that in order to construct a human these are the parameters these are the rules in order for us to instantiate a human to create a human object we need to send our constructor a string an integer and a double value to serve as the name the age and the weight so in order to get this program working within the constructor we need to pass in some matching arguments so we need to send a string an integer and a double value so let's call our human rick and we'll pass in rick for the first argument let's send an age perhaps 65 so he's elderly and then a weight perhaps this will be in kilograms so let's say maybe 70. see now that error went away so we can compile and run this but it currently doesn't do anything so why is this useful why is passing arguments to a constructor useful well this gives us the ability to create different objects that have different attributes so we could create a second human that has a different name a different age and a different weight but we first need to assign these values to the attributes of our class so let's head back to our human class and we need to define a name and age in a way and let's do this outside of the constructor but we're not going to assign a value just yet so let's say string name we're only going to declare this int edge and double weight and then within the constructor we're going to assign all of these values to each of these variables so right now let's say name equals name and age equals edge and weight equals weight now this actually isn't going to work and i'll show you why so let's attempt to access humanone's name and let's do this with a print line statement system.out.printline to access one of these attributes we type in the name of the object dot and whatever we're trying to access let's attempt to access the name now if we were to display the name it says no well what gives that's because we need to assign each of these values to this specific object and one keyword to assign values to this object is to use the this keyword so when we assign name equals name age equals age and weight equals weight we're going to precede each of these with the keyword this this dot name this dot age and this dot weight so for all intents and purposes when we create an instance of a class when we create an object we're going to imagine or pretend that we're replacing this this keyword with the name of the object so if we're creating an object called human replace this or pretend that we're replacing this with human if we created a second human object this could be human too just pretend that but stick with the keyword of this so now if we were to run this human now has a name now let's create a second human so let's repeat the steps human i'm going to call this human2 and rename human as human one just to keep things simple now we can send different arguments to our constructor so we need to send a name an agent away well a string an integer and a double so for our second human let's call him morty we need to send an integer let's say he's 16 and he weighs 50 kilograms and let's attempt to access human two's name so we can do this within a print line statement human dot name and this should display morty so now the point of constructors is that they allow us to construct objects that have varying attributes and in the last lesson when we were constructing cars we were only able to construct blue corvettes and with the help of constructors we can construct objects that have different qualities so they are still humans they're objects that are instantiated from the human class but they have different attributes that make them unique here's one situation that you might run into now how can you access an object's attributes from within its class itself so we can utilize the this keyword to help us with that so let's pretend that humans have two methods they have an eat method as well as a drink method and let's define these both so for eat let's say void eat and within this method let's create a print line statement that says this person's name is eating so in order to access an object's attributes from within its own class we have to use the this keyword so if we want to display the person's name plus maybe some text such as is eating let's type in this dot name to access this object's name plus is eating so this will print the person's name plus is eating and let's try this so let's say human to which is morty is eating so in order to call this method we'll have human2 use its eat method so this will display morty is eating now let's create a drink method void drink and we will display this person's name is drinking system.out.printline this dot name plus is drinking burke and let's say human one which is rick is going to use the drink function because he likes drinking so human one dot drink and here are the results morty is eating and rick is drinking that's because we stated that human 2 is going to use its eat method and human one is going to use its drink method so if we had each of these objects perform the other method a different method let's say human one is going to use the eat method and human 2 is going to use its drink method so now each of these objects is using a different method this time rick is eating and morty is drinking so that's why constructors are useful they allow us to assign different attributes to each object that we instantiate so that's the basics of constructors if you would like a copy of all this code i'll post all of this in the comments down below but yeah that's the basics of constructors in java hey you yeah i'm talking to you if you learn something new then you can help me help you in three easy steps by smashing that like button drop a comment down below and subscribe if you'd like to become a fellow bro [Music] you
Info
Channel: Bro Code
Views: 14,524
Rating: 4.9846005 out of 5
Keywords: Java constructor, Java constructors, Java constructors explained, Java constructors tutorial, Java constructor tutorial, Java, constructor, constructors, tutorial
Id: lhf8gaUx4yU
Channel Id: undefined
Length: 10min 36sec (636 seconds)
Published: Tue Sep 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.