Taking a look at the code for a class

Let's say we are making an app for the military. It is designed for use by senior officers to micromanage their troops in the battle. Among others, we would probably need a class to represent a soldier.

The class implementation

Here is real code for our hypothetical class. We call it a class implementation. Because 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
  }
  
}

The code snippet in the preceding example is the implementation for a class called Soldier. There are two member variables or fields, an int variable called health ...

Get Android Programming for Beginners now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.