 Welcome to Intro to Java Classes and OOP. OOP means Object Oriented Programming for Beginners. Now, this should not be your first Java tutorial. This should be something that you do after you've learned a little bit about Java, but you aren't so sure about how classes and Object Oriented Programming works. It's aimed at beginners, so we'll just walk you through some of the absolute basics such as attributes, and whether they are public or private. A constructor, which is a special type of method that creates an object and you'll see that soon. And behaviors, these are the actual methods of a particular class, and you'll see that in action as well. And then instantiation is where we actually create some objects. We might jump around a little bit to see how things go, but let's get started. So let's take a look here. First thing I'm going to do is I'm going to go ahead, I'm going to create a new Java class. I'm going to call this class person. And I'm going to go ahead and create that. I'm going to save this first. And I'm going to go to my desktop, and I made a little folder for this. And I'm going to call this person.java. So I'm going to click save. So notice this says person, this says person.java. So they have to be the same. So first thing I want to do is I want to create some attributes for my person. Now attributes are the quality, or the qualities that something or someone has. Now a person can have lots of different attributes. The ones I'm going to look at today are very simple. I'm going to do name, and I'm going to do age. Now again, hopefully this is not your absolute first Java video, if it is, this is not the one to start with. But thinking about a person's name, this data type of that is going to be a string. And I also need to put, for now I'm going to put public here, and I'm going to put public int h. So what we've done here is we've created a place in memory for each of these attributes. And we said that the first one is a string attribute, and it is called name. And the second one is an int, or an integer attribute, and it is called age. We've also made them public, but we'll get to private later. The next thing is the constructor. And I'm going to leave that, I'll talk a bit more about that later, but basically constructor is going to look like this. Notice it doesn't have public or private, although it can have public, but it's kind of obvious that it has to be public, which you'll hopefully understand shortly. And then we have some behaviors. Now this is just a kind of a common order for these things. You can kind of mix it around, but it makes a lot of sense to do it this way, and hopefully you'll see why sooner rather than later. So what I'm gonna do is I'm gonna go ahead and I'm going to compile this file, and see what happens. Okay, so compilation finished successfully. Awesome. Okay, I'm gonna go ahead and actually compile and run. And you'll see, we'll get a little error here. It says, main method not found in class person, please define the main method as blah, blah, blah, blah, blah, okay? So this is the standard main method for Java. So I'm gonna go ahead and I'm gonna create a separate file. Okay, I'm gonna go ahead and create that, and I'll come back and explain some of the stuff as well. So I'm gonna call this class person runner. I didn't have to call it that, but that's just what I'm gonna call it. And this is where I'm gonna put my public static void main string args. Again, this is the standard Java thing. And I'm gonna go ahead and save this in the same folder as I just saved my other file documents, Java class OOP. And I'm gonna call this one person runner.java. I didn't have to call it that, but that's what I chose to call it. But whatever I call it, this has to match this. Note the capitalization capital P here, capital R here, and note the capitalization here for person. Okay, so just kind of going back. What I've done is I've created something called a class. And a class is what defines an object, which you'll see in a second. And objects have attributes and they have behaviors. So I've said that a person, for our purposes, a person is an object with a name and an age. So over here in person runner, what I'm gonna do is I'm gonna create some objects. So I'm gonna say person, and I'm gonna say Bob equals new person, like so. Now I'm gonna go ahead and just compile this and see what happens. I should not get any errors. Now if I run this, nothing happens. So let's go ahead and print the attributes. So I'm gonna say system.out.println name plus bob.name. I'm gonna say system.out.println age plus bob.h. Now again, if you haven't done this before, see how name and age match what I have over here. So I'm gonna be printing the name and age of Bob. Now note, I haven't actually defined them yet. So let's see what happens. Okay, compiles, it runs. So you can see how name is null. Null means the value doesn't exist. It's not like an empty string. It's not like quote, quote. It's actually null. It just doesn't even exist. And again, if you're not sure what that means, you're not sure this is the right tutorial for it yet. And now age is zero because ints default to zero. So let's go ahead and give Bob some attributes. So let's go ahead and update the attributes. So I'll say set the attributes. Okay, so I'm gonna say bob.name equals bob. And I'm gonna say bob.h equals, let's say, so I'm gonna save that. I'm gonna compile it and I'm gonna run it. Okay, so now we see that Bob's name is bob. We see that Bob's age is 25. Now we could have made this save, for example, Robert. And then compile it and run it. Okay, this bob here is just a convenience for me. I could have called this person one. One is always nice to have good variable names. Okay, and I compile it. Oops, there we go. I should have changed this to person one. Person one, person one. So let's go ahead and compile it again. And now we have person one is Robert and his age is 25. I'm gonna go ahead and put this back to Bob, but I like that. It's a lot easier for me to remember. Okay, so couple things here. Notice, okay, we've said this is what a person is. A person is an object and this person has, or this object has certain attributes. In this case, a name and it's a string type, an age, and it's an int type. We've done nothing else with it, okay? We have over here, instantiated. Yeah, put that in instantiation. We have instantiated a new person object called Bob. Here we set the attributes and here we printed the attributes, okay? Now the reason we could do this, and this would work, is that over here, we made these attributes public, okay? And what public means is that from outside of the person class, so over here in person runner, it's a different class. I can directly update and access. You may hear it's called mutate or mutator methods. You'll see later, and accessor methods. I can directly update and I can directly access these values. This is a way of doing it, but good object-oriented programming design, especially if you're an AP student. AP requires that you make these private. So I'm gonna go ahead and hit private. We'll see how that affects our program. So I'm gonna go ahead and compile this because until we compile it, it's not gonna make a difference. Then over here, I'm gonna go ahead and compile this again, and let's see what happens. So as you can see here, there are a number of errors. That color's a little bit hard to see with the dark background. But what it says is personrunner.java, that's this file name, nine, this is the line that the error is on. Name has private access in person, and it's pointing to bob.name equals Robert. So name is private, which means over here in personrunner, I can't access it. So this is where we have to do a little bit of extra programming. This is kind of the standard Java way of doing things. Now again, if you're a beginner and you don't care about these things, you can go ahead and just make it public. Nobody's gonna care if you're just experimenting. But if you're actually going to write good software, you're gonna need to make this stuff private. So the way that we get around this, if I can go back to my coding concepts, is what we've seen public and private attributes. The way we get around this is that we create special methods called getters and setters. And I'm gonna show you that right now. So you'll see down here behaviors. Okay, so I'm gonna make this one is gonna be public, which means that over here in personrunner, I can access it. So I'm gonna pay public void. Now, if you know what public void something means, again, this is not the video for you. You need to go back and learn about methods before you can do this. So public void, I'm gonna call this one set name. I'm gonna make this string. Well, there's a couple of ways I can do this. I'm gonna make this string name. And what I'm gonna do is I'm gonna say, well, I'm gonna do it wrong once. So I'm gonna say name equals name. Now, if I try to compile this, what's gonna happen? It actually worked. Who knew? That was not what I expected. I'm gonna put this back here, just see that one later. And I'll explain how this one works. Let's go ahead and compile that. Okay, so this is a special keyword that refers to a particular object. And I'll show you that in a minute. Name, this particular name refers to this variable. However, this name refers to this attribute. Hopefully that, you can see there's a little bit of a difference. So this is the value that we're passing in. And this is the value that is attached to that particular object. So what I'm gonna do is I'm gonna go ahead and change some of these and hide some of these things. And for now, I'm gonna fix them here in a second. Okay, so instead of bob.name equals Robert, which I can't do anymore because it's private, I'm gonna do the following. I'm gonna say bob.setname Robert. So what happens here is name becomes Robert. And then I said this name, so it means this person object's name is equal to this name, which is Robert's. And the reason I put this here is to avoid it, what's called ambiguity. So we got name here and we've got name here. So if I do this, I'm not sure which name this is, which name this is, is it this name, is it that name. So by putting this, I force it to make this particular name equal that. This is one way of doing it. Another way of doing it is to change this to something like new name and then new name. Either way you do it's fine. I prefer doing it with this just because it's similar to how it's done in Python and I do a lot of Python programming. So let me go ahead and compile this one more time, make sure it's working. Let's go ahead and compile this and I'm gonna run it and see what happens. Now we don't see anything, but presumably it's working. So what we have to do now is we have to do a getter method, which is a way of getting the information back. So I'm gonna do public, string and I'll explain why in a second, get name and I'll say return this.name. I could also just do name because it's not ambiguous anymore. There's only one name in this. It's called scope, but I'm gonna keep sticking with this thing. So especially a lot of people I teach and a lot of people online, I know my videos, I do a lot with Python. This is the same as self. So this was Python, this would be self. Now, this.name is a string. So that's why I put string here. That's the return type again. If you haven't done functions and methods, this is probably not the video to start with. And what this lets me do is publicly access these attributes. So I go ahead and compile that. And then over here, I'm gonna say bob.getname. So I'm gonna go ahead and compile it. I'm gonna run it and now I can see name Robert. So notice we are not directly accessing this string because it's private. We are directly accessing this method because it is public. So that is the difference between public and private. Now again, as I mentioned, I could have done name here because this name is the only one and it refers to this name. Now, interestingly, this is private. But because I'm inside the same class, I can directly access this value. I'm gonna go ahead and put this here anyway. So this is only used inside of a particular class to access that class's attributes. So let me do this one more time. So what I did here was I created a new person object called bob and it's a new person. This is blank. I'm gonna explain that in a minute. I have set the name and I have printed the name by using the set here and the get here. Let's go ahead and do the same thing for age. Now notice the pattern. Setters are void because they don't return anything. So public void set age int age. Yeah, you just gotta memorize the pattern. This dot age equals age and then public int get age blank or empty is return this dot age. Again, don't forget to compile it. Now I'm using genie. You might be using something different but this is what I tend to use. And then I'm gonna go ahead and do the same thing. Say bob dot, that's a z, not shift. Set age. Notice that equals its parentheses. And then I'm gonna do system.outprintbob.get age. Okay, so let's compile that, run it. And you can see it works exactly like it did the first time when name and age were private. So so far we've gotten the absolute basics of a class attributes and some basic behaviors. In this case, setters and getters. Okay, so setting the value of a particular attribute. The attributes again are private. So we can't access them directly. This is good object oriented programming standards. And then we have getters which allow us to pull that information back. So we've covered quite a few things here. We've already done attributes. We talked about public versus private. We've done behaviors such as getters and setters and note they are public. We've done instantiation. Now we're gonna look at constructors. Now these two concepts, constructors and instantiation are kind of related. And a constructor is a special type of method that is called one particular time. So when I create an instance. So when I create this Bob instance, this constructor method is called. I'm gonna show you that here. So I'm gonna say system.out.print. I am a constructor. Dot dot dot here, or I'm gonna compile it. Okay, I'm gonna come over to here and I'm gonna compile this again just for good measure and I'm gonna run it. So you see what happened here. I am a constructor. Hear me roar, name Robert, age 25. Did I put print or print line? Print it. Let's go ahead and do that one more time so it's a little more clear. So when I run this and I create an object, this special method is called and that is why you're gonna see that extra line of text printed. I am a constructor. Hear me roar. And then everything else works exactly the same. Now, of course, we probably wouldn't do this. But what we would do is something like the following. We can leave that there. I'm gonna put a second constructor and I'm gonna call this string name int age. I'm gonna say this dot name equals name. This dot age equals age. I'm gonna compile it. Okay, now notice we have two constructors. We have a kind of blank empty constructor and we have a constructor with two values, two parameters, a string called name and an int called age. And what this means is we can do something like the following, say person su equals new person. And we'll say her name is Susan and her age is 30, for example. So what happens in this particular case? Okay, Susan gets sent to here. 30 gets sent to here. And then they are assigned to here. So this becomes Susan and then this one becomes 30 in this particular case. So I'm gonna go ahead and copy this and I'm gonna change that to su. Change that to su. Okay, so I'm gonna go ahead and compile it and run it. And you can see here I've got Robert age 25 and I got Susan age 30. So what this lets us do, you can see there's two different ways. I can instantiate my object with no parameter in which case name is null as I showed you earlier and age is zero. And then I can set the attributes one by one like this or now with my new constructor, I send a string and an int in that order. It's very important. This is actually called the signature. So the signature here is name or sorry, is string and int. And then I add to apply those values or update those values to that particular object using this. Again, I could do something like instead of this, I could do like my name. You'll see this pattern and I could do name equals my name and it works pretty well. So I could do it like that. Again, I personally prefer this style because it's very clear. I don't have to get confused about this name, that name, my name, new name, et cetera, et cetera. As long as I use this, it helps me keep organized. I know that this applies to the attributes of this particular class. Let me go ahead and compile that again. Yeah, screw that up. It helps me avoid stuff like that. So let's go ahead and compile it and over here you can see how. Now we have different ways of instantiating a particular object, okay? Now I could also do something like this. Get a little bit lazy here. I could do this, I could just get rid of that. And let's say we don't know the person's age and it'll just be default to zero. So I could do something like person, Jason, who's new person, Jason. And now this is just a string. So it will go to this constructor instead of that constructor, instead of this constructor. So it's based on what's in here and that is called signature. So I'm gonna go ahead and compile that. I'm gonna go ahead and run that. Let's see, actually let's go ahead and do jason.setAge. Make Jason a little bit older. And then down here, make this jason.getName and jason.getH. So I'm gonna compile it, I'm gonna run it and you'll see how that works. So we've got different ways of doing these things based on different ways of instantiating an object based on the way we create the constructor. And there's more that we can do in these things but this is a pretty good overview of how classes are created, of how objects are instantiated. And what's nice about this is if we decided later to add a different thing. So private, let's say string, I'll say blood type. And I choose blood type because I live in Asia and blood type's a big thing over here. So by adding that, automatically, the Bob, Sue and Jason object get that attribute. Now we have to set it and everything like that but a space is created in memory for that information to go there. Now I'm gonna delete that because we don't need that but you get hopefully the idea. Then I need to create setters and getters for blood type et cetera, et cetera. And then I can start playing with the signature here and the constructor, adding blood type et cetera, et cetera. Did you notice I like the word et cetera, et cetera. So let me just go back to the coding concepts. And so what we looked at today were attributes of a class, public versus private. And again, you can have public attributes. It's just not considered good object-oriented programming practice, especially if you're an AP student. AP expects you to make your attributes private and they do take off points for that. So be careful there. Talk about constructors. Constructors can have no arguments. They can have one argument. They can have two arguments. However you wanna do it. Behaviors and we looked at setters and getters today. And we can of course have other methods, other behaviors. That's what makes object-oriented programming very powerful. But yeah, that's something we looked at today. And just try to, and also we looked at instantiation which is creating objects. But one thing, just keep in mind there's a certain pattern for these things. So attributes, private type name, private type name. Constructor, we can have an empty constructor like kind of the default constructor with no attributes. We can have one attribute or one parameter sent to it. We can have two parameters. It's really up to us as a programmer what we wanna do. And then the behaviors, generally speaking, we have setters and getters for most of our attributes. And again, this is the Java standard way of doing things. So anyway, hopefully that gave you a little bit of an idea of how object-oriented programming works. You may not get an idea of exactly how you wanna apply it, but this is the format for a basic class in Java. So notice we didn't use static here because static doesn't really work with objects. It has a different usage. The only static we have is over here with our public static void main. But it's something you can mix and match, but that's a bit more advanced. So hopefully for beginners, this gives you a little bit of an idea of how to create a class for an object, how to instantiate those objects, different ways, how to set attributes, how to get attributes, and just kinda how to play around a little bit with objects. So hopefully in part two, I'll get to some day. We'll talk a little bit about inheritance and a couple other things that you might need to know. Okay, so anyway, I'm Tokyo EdTech, keep on coding.