- Learning Java by Building Android Games
- John Horton
- 626字
- 2021-07-23 19:02:31
Declaring, initializing and using an object of the class
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:
- In the first part of the code,
Soldier mySoldier
declares a new variable of typeSoldier
calledmySoldier
. - The last part of the code
new Soldier()
calls a special method called a constructor that is automatically made for all classes, by the compiler. This method creates an actualSoldier
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. - And of course, the assignment operator
=
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. - Consider this visually with the next image.
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.
Tip
We can think of a class' methods as what it can do and its instance/member variables as what it knows about itself. Methods act on data, variables are the data.
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.
Note
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.