Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Creating Variables
- Creating Class Variables
- Creating Behavior with Methods
- Putting One Class Inside Another
- Using the this Keyword
- Workshop: Using Class Methods and Variables
- Summary
- Q&A
- Quiz
- Activities
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
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:
- Line 5: An array of Virus objects is created with the numViruses variable determining the number of objects in the array.
- Lines 6–7: A for loop is used to call the constructor method for each Virus object in the array.
- Lines 8–9: 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 application was run.
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.
Summary | Next Section

Account Sign In
View your cart