 Welcome to intro to Java with an emphasis on AP computer science a with me Tokyo ed tech today's topic writing objects And we're gonna be working on non-static methods. Let's take a look at the concepts will be Focusing on so as I mentioned non-static methods, we're gonna take a deep dive into constructors here all along We've been using what's called the default constructor, but today we're gonna actually go ahead and make our own constructors And then we're gonna do an example with a printing card So like for a deck of cards and then we want to learn about something called the two string method So let's go ahead and get started. So let's let's go ahead and take a look at the person class This is from a previous video. So our person has two private attributes a name and an age So we have a string and an int we have our public setters and we talked about validation in the other video We have our public getters now These are also non-static methods and by non-static they refer to a specific object So let's go over here and just kind of do that for review. So we've been doing this all along. So a person iron man equals a new person And we'd say iron man dot sets name Tony Stark and we could do if we wanted to we could also do you know iron man dot sets age And that would be 53 and so what we want to do here is we want the iron man object To be able to introduce itself So I'm gonna go back over to here and I'm gonna add a method down here So it's called non-static method And so it's gonna be public because again, we want this to be accessible from a different class Void because we're not gonna return anything and it's gonna be say greeting and we're not gonna send any values So it should be really simple system dot out dot print ln and Just say hi. My name is quote plus And what we just do here is we do name Plus quotes don't forget the space and nice little period there Semi-colon Okay, so let's go to compile that make sure it piles so far so good So now what I could do is over here. I can say iron man dot say greedy You know how that popped up because the computer knows it exists So if I run this you see hi, my name is Tony. I put an extra space in there Oh And hi, my name is Tony, so if we had gone ahead and done Captain America America Steve So we can do here I'm so copy and paste America American Captain America Dot say greeting you can see that it's a little bit different. So because these are non-static methods They refer to a specific object so in this case the name is The name of that particular object so in the case of iron man It's Tony case of Captain America. It is Steve. Okay, so when we say non-static We mean it refers to that particular Object and not part of the you know the class itself So now we want to move on to so that's what we've done before it's based There's nothing really new there So we want to look at this class here called card and we're gonna be working with you know playing cards like I usually use The ace of spades as an example, so I've got a class. It's got a private attribute for names Got a private attribute for suits We're gonna be talking about constructors today. I've got my setters and I've got my getters So let's go ahead and create a card. So I'm gonna say card Call card equals new card And I can say card dot set suit or set name and String and I'm gonna do card dot set suit And I actually copied it over here So I had it and that is the ace of spades and so if you're a Motorhead fan, you know why it shows that So let's go ahead and compile that Okay, now there's we're not printing anything out at this point, so we shouldn't see anything so You can see here. We've created this card object And what we haven't really talked about before is how that works So if I go over to card dot Java, there's a special method Special type of method called a constructor So there's one that is like this And this is called the default Constructor so we don't actually have to put it in Java will add that for us So this is the default constructor default So this default constructor takes no arguments. So the signature is empty So in here right now there's nothing we could put some code in here. So what happens is when we construct a new card This method is called so I could put in here system dot out print ln. I am a new card Just to show you that this is how this works You see here. I am a new card because when we did this You know instantiation this method is called So what we want to do so with that knowledge is we can make our own non default constructors So I'm going to go ahead and comment that out So what we want to be able to do is card card not dart card equals new card a and That's paid What we want to be able to do is this So now you can see that the signature has changed Okay up here. It was an empty signature. So this just goes back to method overloading This is just another type of method overloading, but it's it's a very specific method called the constructor So this is a string and this is a string. So if I go over to here I can go ahead and make a new constructor notice. I didn't put public here I didn't put void or any that kind of stuff. I just put the name of the class So string name and string suit So then what I do inside of this I do this dot name equals name This dot suit equals suit So now I've got two constructors One with an empty signature one with a signature of string string So in this case I've got string string. So it's going to call the non default constructor Let's go around that and now we don't see anything Because this didn't this didn't print out because we didn't go here. We went here So let's go ahead and test this So what we want to do is we want to be able to print the card So what I want to be able to do is go like card dot print card like that So I'm going to go over to my card class I'm going to go public void print card Now what I could do is something very simple. I can do system Dot out that print l n this dot Name plus this dot suit Let's compile that Let's run it and you can see now card dot print card actually prints out asus space That's nice. I want to make it look really nice So what I want to do is I'm going to do a little bit different here. I'm going to do quote quote plus plus minus minus minus And I'm going to make a nice little card Graphic here. So let's go ahead and do that And so what I want to what I want to happen. I'll show you here Is I want to be able to print it out like Basically, this will be spade Actually, this is actually how I would actually code this because I want to get the formatting right spade and then this is an ace This is a spade. So let's go ahead and test it real quick Okay, so you can see ace of spades, which is which is what we wanted Is that what we wanted? Yeah, I guess that's what I wanted So now of course if the card is in ace of spades, that's not going to work So what we got to do here is I'm going to replace this with quote quote plus plus this dot suit I'm going to replace this with quote quote plus plus this dot name And I replace this with quote quote plus plus this dot suit Let's go ahead and compile it test it And you can see it's still working. Now, let's go ahead and test that with a different card. Let's go ahead and do the Jack of spades just to make sure it's working. Oops. Just make sure it's working how we expected compile it And you can see that we have jack now I should probably test this with one of the other ones, but we'll let that one go I like to ace because we don't like the ace of spades I was actually standing on the line with Lemmy one time at the Bucharest airport. I was like three feet away from Lemmy a legend um anyway He was ordering a glass of wine As befits a very cool rock star. Anyway, uh, so non-static methods So basically you see how everything that pertains to that card object as much as possible We want to put it into that card class That's part of that encapsulation idea where the object Is is a self-contained unit and because because we've done it this way I can take this card class and put it into another program and it'll work exactly as You know exactly the same and this this really helps with code reusability And yeah, that's that so our final topic is a method called to String this is a special method in java and uh All objects have this two string method. There's a default two string method that is included for you So let's go ahead and try this. Let's go ahead and do system dot out dot print Ln print ln card So this two string method is a special method it is called when we print an object Let's go and see what it is printed. What do you think is going to be printed? That's that's a good question, I suppose Can you see here? We got card at 764c12 b6 now that will that will differ Uh on your computer, but you'll see something very very similar to that And what this says is this is a card object at I believe this is some kind of memory location It might be or some kind of reference to that particular object. That's why it changes So this is the default output But what we probably want to do is something like this Is to make our own two string method so public uh string two string And how it works is we return what we want to be printed. So I'm going to go ahead and do this dot suits or this dot name plus this dot suit So that allows us to control How an object is printed So we go ahead and compile it And let's go and see what happens here And you can see we have the ace of spades So you need to know how to be able to write a two string method again We don't put system dot out dot print line this plus name this plus suit. We return a string So that's kind of important now we could actually you know assemble this we could return instead of printing it We could add all this together use the new line character and return that as two string That would be an interesting exercise. I think if you were to tackle that Probably a good practice So that is that that's basically what we need to know about non-static methods So these non-static methods are attached again to the objects So each object has its own attributes its own set of Informations its own state you might be here called And then we have default constructors Where we don't have any attributes We can make our own custom constructors that are called based on the signature We can add non-static methods to our objects And we have the specific method called two string that always returns a string Is how we want to print out our object So that I think is yeah, I think that was a good that was a good session. Uh, thanks for stopping by take care