/** * Purpose: A class from which objects might be created, which behave like * sheep. Demonstrates use of classes to represent classes * of objects in real life, and the difference between using * or omitting static. */ public class Sheep { // each sheep is kept in a pen for which the number is private int penNumber; // total number of sheep created from this class private static int totalSheep = 0; // sheep constructor public Sheep(int n) { penNumber = n; totalSheep ++; return; } // find which pen a sheep is in public int find() { return penNumber; } // move sheep to another pen public void moveTo(int differentPen) { penNumber = differentPen; return; } // count all sheep // NB: this is a static method, and it doesn't // use any non-static variables public static int countAll() { return totalSheep; } }