Looking at the code for a class

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.

Class implementation

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 ...

Get Learning Java by Building Android Games - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.