Looking at the code for a class
Let's say we are making an app for the military. It is to be used by senior officers to micro-manage their troops in battle. Among other things, we would probably need a class to represent a soldier.
Class implementation
Here is some real code for our hypothetical class. 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! } }
What we have here is a class 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, which is what we discussed in Chapter 9, Java Methods.
To...