Sams Teach Yourself Java 2 in 24 Hours

Sams Teach Yourself Java 2 in 24 Hours

By Rogers Cadenhead

Workshop: Using Class Methods and Variables

At the insistence of every attorney and management executive in the Pearson family of computer publishers, 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 Virus.java. Enter Listing 11.1 into the word processor and save the file when you're done.

Example 11.1. The Full Text of Virus.java

 1: public class Virus { 
 2:     static int virusCount = 0; 
 3: 
 4:     public Virus() { 
 5:         virusCount++; 
 6:     } 
 7: 
 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 application 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 VirusLook.java when you're done.

Example 11.2. The Full Text of VirusLook.java

 1: 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:             System.out.println("There are " + Virus.getVirusCount() 
 9:                 + " viruses."); 
10:         } 
11:     } 
12: } 

The VirusLook class is an application 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 application:

java VirusLook 200 

Arguments are read into an application 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 application:

If the numViruses variable is not greater than 0, nothing happens in the VirusLook application.

After you compile the VirusLook.java file, test it with any command-line argument you'd 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 application. On the author's system, anything above 5.6 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 125000 viruses. 

Share ThisShare This

Informit Network