Let's say we are making war simulation game. It is a game where you get to micro-manage your troops in battle. Amongst others, we would probably need a class to represent a soldier.
Here is real code for our hypothetical class for our hypothetical game. We call it a class implementation. As the class is called Soldier
, if we implement this for real we would do so in a file called Soldier.java
.
public class Soldier { // Member variables int health; String soldierType; // Method of the class void shootEnemy(){ // bang bang } }
Above is an implementation for a class called Soldier
. There are two, member variables or fields, an int
variable called health
and a String
variable called soldierType
.
There is also a method called shootEnemy
. The method has no parameters and a void
return type, but class methods can be of any shape or size that we discussed in Chapter 4, Structuring Code with Java Methods.
To be precise about member variables and fields, when the class is instantiated into a real object the fields become variables of the object itself and we call them instance or member variables.
They are just variables of the class whichever fancy name they are referred to by. Although the difference between fields and variables declared in methods (called local variables) does become more important as we progress. We will look at all types of variables again later in this chapter.
Remember that Soldier
is just a class, not an actual usable object. It is a blueprint for a soldier, not an actual soldier object, just as int
, String
and boolean
are not variables they are just types we can make variables from. This is how we make an object of type Soldier
from our Soldier
class.
Soldier mySoldier = new Soldier();
The previous code can be broken into three main parts:
Soldier mySoldier
declares a new variable of type Soldier
called mySoldier
. new Soldier()
calls a special method called a constructor that is automatically made for all classes, by the compiler. This method creates an actual Soldier
object. As you can see, the constructor method has the same name as the class. We will look at constructors in more depth later in the chapter.=
in the middle of the two parts assigns the result of the second part to that of the first. The next image summarizes all this information.This is not far from how we deal with a regular variable. Just like regular variables, we could also have done it in two parts like this.
Soldier mySoldier; mySoldier = new Soldier();
This is how we could assign to and use the variables of our class.
mySoldier.health = 100; mySoldier.soldierType = "sniper"; // Notice that we use the object name, mySoldier. // Not the class name, Soldier. // We didn't do this: // Soldier.health = 100; // ERROR!
Above, the dot operator .
is used to access the variables of the object. And this is how we would call the method. Again, by using the object name and not the class name and followed by the dot operator.
mySoldier.shootEnemy();
We can summarize the use of the dot operator with a diagram.
We can also go ahead and make another Soldier
object and access its methods and variables.
Soldier mySoldier2 = new Soldier(); mySoldier2.health = 200; mySoldier2.soldierType = "commando"; mySoldier2.shootEnemy();
It is important to realize that mySoldier2
is a totally separate object with completely separate instance variables to mySoldier
.
What is also key here is that this previous code would not be written within the class itself. For example, we could create the Soldier
class in an external file called Soldier.java
and then use the code that we have just seen, perhaps in our SubHunter
class.
This will become clearer when we write our first class in an actual project in a minute.
Also, notice that everything is done on the object itself. We must create objects of classes to make them useful. Classes do not exist while the game is running, only the objects made from the classes. Just as we learned that a variable occupies a place in the computer's memory, so does each and every instance of an object. And it, therefore, follows that the member variables contained within the objects are therefore contained within that memory too.
As always there are exceptions to this rule. But they are in the minority and we will look at the exception later in the chapter. In fact, we have already seen an exception in the book so far. The exception we have seen is the Log
class. Exactly what is going on with these special methods known as static methods will be explained soon.
Let's explore basic classes a little more deeply by writing one for real.
13.59.100.205