/* * Foo.java * Illustration of a program with two classes. * This must be compiled together with Bar.java: javac Foo.java Bar.java */ public class Foo { // Example of a static or class variable. private static int numberOfFoos = 0; // Instance variables. private int fooID; // ID number of an instance. private Bar fooBar; // Illustrate use of another class. // Constructor. public Foo(int id) { numberOfFoos++; // Count the new instance we are creating. fooID = id; fooBar = new Bar(id); } // Static method to access a static variable. public static void printFooCount() { System.out.println("The number of Foos is " + numberOfFoos); } // Static method to print out a number of foos. public static void printSomeFoos(int n) { for (int i = 1; i <= n; i++) { System.out.print("foo "); } System.out.println(); } // Instance method to access an instance variable. public void printFooID() { System.out.println("The ID of this foo is " + fooID); } // Main method to demo this class. public static void main(String[] args) { printSomeFoos(5); System.out.println("Before:"); printFooCount(); Foo foo1 = new Foo(10); Foo foo2 = new Foo(20); System.out.println("After:"); printFooCount(); foo1.printFooID(); foo1.fooBar.printBarID(); foo2.printFooID(); foo2.fooBar.printBarID(); } }