 Welcome to Intro to Java and AP Computer Science A. This lesson is about using objects. This is part one and in this lesson we're just gonna learn how to use objects to store data. And so we're gonna look at what is an object. We're gonna talk about dot notation, which you've seen if you've watched the other videos. We're gonna talk about storing data with primitives and you know we've done that. And we're gonna look at you know why it's not always a good idea to do it that way. Just you know a separate variable for everything. Let's see what I'm talking about a little bit. Now we'll talk about how to create some objects. There's a coding side and then there's like the instantiation side which is also part of the code. So yeah, get ready. Here we go. So first part, using objects and dot notation. So in previous units we've seen this as for example string, name equals quote Tokyo EdTech. So in this case you can see that string is capitalized. So this tells us that string is a class and class is what defines an object. And then in this case the object, the variable name is called name. And then basically the value is Tokyo EdTech. So the question is what makes this an object? Why do we say it's an object? Well an object is a structure that has basically attributes and methods. So it has some values inside of it, has some data inside of it, and it has some methods that let us do something with that data. So for example we could do the following. We could do int length, length equals name dot length. So you see here we have the object which is called name, and then we have this method called length which returns the number of characters in it. So we can print that out, system.out.println, length. So let's go ahead and compile. Let's get practice to get into what I compiled. It's successful and I'm going to, what did I do wrong? Okay so it says please define the main method as public static void main string args. Okay I missed the brackets there. So it is good practice to read your error messages. Okay so you see here it says 11 because there are 11 characters in Tokyo EdTech. So we have the data side of it which in this case is the value, the string value of Tokyo EdTech. But we also have some methods that let us access that information. So we have name dot length. This is dot notation. So in this case it's an object dot method. So next part is storing data with primitives. So this is again this is something that we've done already and there's nothing wrong with it. But as your program gets bigger as you have a lot more information there are some problems with this. So for example let's go ahead and take a look at the following. So let's go ahead and do string first name equals say Tony. String last name equals Stark. I hope the Marvel guys have a sense of humor don't sue me. Int age 53. Double height equals 183.0 or 185.0. Arpeggio must be very tall. Unless that's the common version. Boolean is alive equals false as Tony Stark. Sacrificed himself for humanity at the end of the game. So you can see here we've stored some data in a bunch of primitives. Now which in not a problem but let's say as we know Tony Stark is not the only Avenger. So let's say we wanted to do Captain America. So Steve Rogers ages 34 but I think he's actually much older because of all the time travel shandigans. And let's see I also have a list of 185. Not sure that's correct. Anyway that's what matters. For our purposes it's so true. So now we have a problem because we have two variables that have the same name. So as soon as I do this this is erased. So what I'd have to do is to two and two. I can do this. It's not a problem. Then we have another Avenger, another Avenger, another Avenger. Then we would say we have first name 50. So it's gonna quickly very very quickly get out of hand. Okay. So what we want to do is we want to avoid this problem of having two, two, two, three, three, four, four, five, five, five, etc. etc. Because you know who is first name two. Was that Tony Stark or was that Captain Rogers? Was it the Black Widow? I don't know. So this is where using objects to store data comes in. So what we're gonna do is we are going to basically create one object that contains a multiple values. So what we want to end up being able to do is the following. We want to be able to do something like this. Iron man that first name equals that's what we want to end up being able to do. Iron man dot last name equals Stark. This is what we want to end up being able to do. And I say iron. Well, let's do a little bit here. And then we want to be able to do we want to do captain captain America first name equals Steve captain America last name equals Rogers. I think you can already see how this is easier. We've clearly identified this as Iron Man. And then we said well Iron Man's got a first name Iron Man's got a last name. And we'll go ahead we do age and all that. We'll do all that a little bit here. Okay. So to be able to do this. Okay. To be able to do this. Now I'm gonna go ahead and comment this out because we can't do that yet. If we try to compile it with an error. Let's go ahead and compile it. Just make sure the code is working. Okay. So this is what we want to be able to do. So to do that, we have to first tell the computer what attributes that this object will have. So one of the big issues or one of the big challenges in coding coding well is choosing good variable names and choosing names for things. So Steve Rogers and Tony Stark and you know Black Widow and all the characters. You know what are they? Now we call them superheroes. There's also people in the Marvel Universe that aren't superheroes. So they're people. We're just gonna call them people. So we're gonna go ahead and make a new Java file. I'm gonna save it into the same folder. I'm gonna save this to the same folder. I'm gonna call this person.java. So now watch what I do. I'm gonna make a class called person. This is no different to what we did before. But this part is different. This program using objects one Java is our runner. It's what we call a runner. It's gonna run our main program. That's why we have public static void main string arcs. Person.java on the other hand does not have a main method because it doesn't execute by itself. So what we need to do is tell the computer what a person is, what their values are, and what their attributes are. So watch what I do. I'm gonna type public string first name equals quote-quote. Notice there's no space here. This is an empty string. Now I could have done this and basically what we'll do it would create what's called a null object. We're not ready for that yet. So just to kind of keep you know keep our code kind of simple and understandable at this point we're gonna stick with this. So public string last name. I'm gonna do public int h equals zero. That's gonna be my default value. Public double height equals zero point zero. And public double was it? Oh sorry it's a Boolean. Boolean is alive equals false. That's gonna be our default value. That's it. So what we've said here is we've said that a person is an object. The class defines an object. Any person object will have five attributes. These attributes are first name, last name, age, height, and is alive. First name to string, last name to string, age is an int, height is a double, is alive is a Boolean. Now watch I'm gonna go ahead and compile this. Hopefully you should see compilation finish successful. Now if I try to run this you're gonna get an error. It says error main method not found in class person. So because there is no main method Java doesn't have any place to start. So now, now that I've done this, I can go down to here which is how to create an object. So what I'm gonna do is I'm gonna use this pattern. So it is person. I'll say iron man equals new person. So what I've done here is I've created a variable called iron man. I have said that iron man is a person and this person is a new person object. It hasn't been created before. I'm gonna compile this and just make sure it works. Make sure it compiles. Okay. So compilation finish successful. This tells me the code itself. There's nothing syntactically wrong with it. So now if I did this if I said system.out.println iron man dot let's do age. You'll see zero. That's because person.java by default when we create the object sets the age here to zero. Now if I change this to 100, I want to do that. But if I did, and if I execute this, now it's 100. So what I've done is I've set my default values here. So when I create an object, we don't we don't have maybe we don't have all the information we do. We have to add that. You'll learn a lot more about this later in this course. This is just really just to kind of get started. The thing here where it says public just a quick side note. There's there's different types of what's called access. There's public and there is private. I say public with that one. And I go private. So what happens here? I'm going to compile this. So now this one's private. I'm gonna go back to here. I'm gonna click compile. And it gives me an error. Age has private access in person. So what that means is the variable age, this attribute called age, because it's private, I can only use it inside this class. So I could use it like you know I would do this but I could do system.out.printlnh. And that would work because it's private and I'm in the same class. Over here, I'm in a different class. This class is called using objects one. So because this is private, I can't access it from outside. Now, especially your AP computer science students, everything will have to be private. We'll learn more about how to do that later. But for now, this will get us started. Now, you notice we are also going to recompile that and run it. Make sure it is working. Okay. So what we need to do now is to assign our variable values. Now, I'm kind of a kind of a lazy guy. So I'm just gonna go ahead and copy this. And now notice I don't need to do string first name. I want to do iron man dot first name. Because I'm not I'm not instantiating the variables. I am just assigning values at this point. Okay. So the type has already been declared over here in the class. So now I can do copy that, paste it iron man dot age, say last name. That's the first name. Last name, age, height, if I go ahead and compile that again, and then if I run it, you can see down here, 11 still blood is coming from up here still. So iron man first name is Tony, his last name is Stark, age is 53, height is 185, and is alive is false. And there you have it. That's that's pretty much, that was pretty much the whole idea behind this lesson. Now, if I wanted to create a person for Steve Rogers, I could do person captain, Captain America equals new person and say that you know, Captain America dot first name equals Steve, etc, etc, etc. So now I can create as many people as I like. And just you can just see how much better and well organized this code is compared to having to do first name, first name two, first name three, first name four, first name five, etc, etc, etc. So that's one of the benefits of using a class based organization. So I'm going to go ahead and delete this and so yeah, that's that. Yeah, I think that's that's enough. I think that's what we wanted to cover in this lesson is pretty quick one. So let me go back to the voting concept. So we looked at what is an object. So an object has attributes and methods. So our string objects had method like length. And the object that we just created the person object only has attributes, we're going to learn how to make methods later dot notation. So it's the object dot attribute in this case or object dot method. We reviewed storing data with primitives that was first name, first name two, etc, etc. And you can see again, you know, if you have, you know, 100 people, then you're gonna need 500 different variables. So with a class based thing, you have five variables with 100 different objects. So it's a lot easier to manage your data in that in that fashion. And then we looked at how to create an object. We looked at the coding. And again, for this part of it, it's very, very simple, we go back to the main screen. So for this part of it, it's very, very simple. It's class, you know, whatever you want to call that class, in this case, person, and we're gonna use public, the data type, the name of the, the variable name, and then we gave it a default value. And then we also did how to instantiate objects, which was, you know, the basically the pattern is we'll put that in there for you. Pattern is this, and you'll see this over and over again, it's class name, you know, object name equals new class name. So that you'll see that pattern over and over, over. As a quick aside, we could also do strings and strings are objects, we could do, we could create a string like, so string name equals new string. And then I say Tokyo edtech. And then we'll get we'll get to this part again, also later, and why sometimes we have values here, and sometimes we don't, you got a lot to look forward to. So that is that. That's the lesson. Thanks for, thanks for tuning in. And yeah, keep on coding. Take care.