A class is a blueprint that describes the data and behavior. Simply, it is a collection of variables(data) and methods(behavior) that together solve or serve a specific purpose.
Classes allow us to package data/behavior so that we can reuse them later. The data/behavior tend to serve a theme goal.
You already have experience writing a class. All the code we have written has gone in a class. Now we are just adding multiple classes to our programs.
An instance is a copy of the class that can be manipulated and changed in its own way. When we create an instance of a class, we call this instantiate.
Think of a class like a blueprint for a house. We have written how everything should look but we haven't implemented one yet.
Think of an instance as an implementation of that blueprint. We are creating a copy that is just like the blueprint but we are making minor changes to each one.
1. Open a new file: Give it a visibility (for now use public), then java keyword class, give your class a name, and then {}. Remember to capitalize the first letter and to not start with a number. Make sure to name the file exactly the same but with the .java extension.
2. The next part you will need to create is the constructor. The constructor is a special method for a class, when you create an instance of that class, it'll be the first method that is run.
To create a constructor, you set a visibility (for now use public), then the exact same name as the class, then () and {}. There is no return type set for a constructor!
If you need any parameters, they will go inside the ().
3. You have made the most basic version of a class you can at this point. Self-five.
1. You don't have to have a constructor, java will make a default one if you do not put one. However, if you don't create one, then you can't send arguments to your class.
2. Class name MUST match file name.
3. Constructor name MUST match class name.
1. You have already made so many instances, you just haven't realized it.
To create an instance of a class you follow this structure: ClassName instancename = new ClassName();
Just like how you have used Scanner & Random in the past.
The first ClassName is exactly what it sounds, letting java know what class we are referring to.
The instancename is the name we are giving it so we can refer to it.
Then the assignment operator; the equal sign.
The java keyword new which then calls the constructor ClassName() to start the implementation of the class and create our own instance.
2. Now you have your own instance of that class!
1. You can create as many instances of a class as you want.
2. Make sure that they class you are trying to create an instance of is located in the same folder you current file is in.
1. Classes allow us to package up methods/variables into a single file that we can create instances for in any program we are using. For instance, if I'm writing a game and I need a class to manage players, I could potentially write that a single time and implement it many times!
For this Quick Code, you will start it!
1. Create a class and call it Player.
2. Create a constructor that accepts several arguments. A String for gamerTag and an int for playerNumber;
3. Inside the constructor print out the players name and what player number they are!
4. Create a Main class with a main method to test your Player class.
5. Create two instances with different player names and numbers.