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
- 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
Creating Class Variables
When you create an object, it has its own version of all variables that are part of the object's class. Each object created from the Virus class of objects has its own version of the newSeconds, maxFileSize, and author variables. If you modified one of these variables in an object, it would not affect the same variable in another Virus object.
There are times when an attribute has more to do with an entire class of objects than a specific object itself. For example, if you wanted to keep track of how many Virus objects were being used in a program, it would not make sense to store this value repeatedly in each Virus object. Instead, you can use a class variable to store this kind of information. You can use this kind of variable with any object of a class, but only one copy of the variable exists for the whole class. The variables you have been creating for object thus far can be called object variables, because they are tied to a specific object. Class variables refer to a class of the objects as a whole.
Both types of variables are created and used in the same way, except that static is used in the statement that creates class variables. The following statement creates a class variable for the Virus example:
static int virusCount = 0;
Changing the value of a class variable is no different than changing an object's variables. If you have a Virus object called tuberculosis, you could change the class variable virusCount with the following statement:
tuberculosis.virusCount++;
Because class variables apply to an entire class instead of a specific object, you can use the name of the class instead:
Virus.virusCount++;
Both statements accomplish the same thing, but there's an advantage to using the name of the class when working with class variables. It shows immediately that virusCount is a class variable instead of an object's variable, because you can't refer to object variables using the name of a class. If you always use object names when working with class variables, you won't be able to tell whether they are class or object variables without looking carefully at the source code of the class.
Creating Behavior with Methods | Next Section

Account Sign In
View your cart