Workshop: Using Class Methods and Variables

At the insistence of every attorney and management executive in the Pearson family of computer uploaders, the workshop for this hour will not be the creation of a working virus program. Instead, you'll create a simple Virus object that can do only one thing: count the number of Virus objects that a program has created and report the total. Load your word processor and create a new file called Enter Listing 11.1 into the word processor and save the file when you're done.

Listing 11.1. The Full Text of
 1: public class Virus {
 2: static int virusCount = 0;
 4: public Virus() {
 5: virusCount++;
 6: }
 8: static int getVirusCount() {
 9: return virusCount;
10: }
11: }

Compile the file and return to your word processor. To test out this new Virus class, you need to create a second class that can create Virus objects. The VirusLook class is a simple app that creates Virus objects and then counts the number of objects that have been created with the getVirusCount() class method of the Virus class. Open up a new file with your word processor and enter Listing 11.2. Save the file as when you're done.

Listing 11.2. The Full Text of
 1: public class VirusLook {
 2: public static void main(String[] arguments) {
 3: int numViruses = Integer.parseInt(arguments[0]);
 4: if (numViruses > 0) {
 5: Virus[] virii = new Virus[numViruses];
 6: for (int i = 0; i < numViruses; i++) {
 7: virii[i] = new Virus();
 8: }
 9: System.out.println("There are " + Virus.getVirusCount()
10: + " viruses.");
11: }
12: }
13: }

The VirusLook class is an app that takes one argument when you run it at the command line: the number of Virus objects to create. The following is an example of a command that can be used to run the app:

java VirusLook 200

Arguments are read into an app using a string array that's sent to the main() method. In the VirusLook class, this occurs in Line 2. To work with an argument as an integer, it must be converted from a String object to an integer. This requires the use of the parseInt() class method of the Integer class. In Line 3, an int variable named numViruses is created from the first argument sent to the program on the command line. If the numViruses variable is greater than 0, the following things take place in the VirusLook app:

  • Line 5: An array of Virus objects is created with the numViruses variable determining the number of objects in the array.
  • Lines 6–8: A for loop is used to call the constructor method for each Virus object in the array.
  • Lines 9–10: After all of the Virus objects have been constructed, the getVirusCount() class method of the Virus class is used to count the number of its objects that have been created. This should match the argument that was set when the VirusLook app was run.

If the numViruses variable is not greater than 0, nothing happens in the VirusLook app. After you compile the file, test it with any command-line argument you would like to try. The number of Virus objects that can be created depends on the memory that's available on your system when you run the VirusLook app. On the author's system, anything above 5.5 million viruses causes the program to crash after displaying an OutOfMemoryError message. If you don't specify more Virus objects than your system can handle, the output should be something like the following:

There are 5500000 viruses.