Step 6

Creating the Boxer Class


Next up, let’s create another new Java class, call it “Boxer” (or any other type of Fighter you can think of, you’ll see why in a bit).

We’ll be using this class to create a specific Fighter type that will already have their Attacks every time it is constructed.

Creating static class variables


Next, add the code as shown here to create a new special type of class variable, a static variable.

A static variable is a variable that is “shared” between all instances of the class. For example, if we were to make the health of a Fighter a static variable, all Fighters would have the same health, and lose/gain health at the same time. This is very powerful, but we need to be careful because, like with health, some variables really shouldn’t be static.

However, in this case, static variables are very useful for us. Here, we use them to create some pre-made Attacks that all Boxers will have.

We also have an error in our code at this point, this is fine. Just like before, it is because we need to use the constructor of the original Fighter class. We’ll fix this next.

Creating the Boxer constructor


Next, add the code as shown here to complete the constructor for the Boxer class.

Just like before, we use super to use the Fighter constructor because we are extending the Fighter class. (We can’t create a Fighter without creating a Boxer!)

Then, we add the static Attacks into the Boxer’s “attackList”. With this constructor, every boxer will always start off with our 4 attacks!

This is actually all the code that we need for our “Boxer” class! Next we’ll go over how to use this, as well as the AdvancedAttack class.