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
What Objects Are
As stated, objects are created by using a class of objects as a guideline. The following is an example of a class:
public class Modem {
}
Any object created from this class can't do anything because it doesn't have any attributes or behavior yet. You need to add those or this class that won't be terribly useful. You could expand the class into the following:
public class Modem {
int speed;
public void displaySpeed() {
System.out.println("Speed: " + speed);
}
}
The Modem class now should be recognizable to you because it looks a lot like the programs you have written during Hours 1 through 9. The Modem class begins with a class statement, as your programs have, except that it has a public statement alongside it. The public statement means that the class is available for use by the public—in other words, by any program that wants to use Modem objects.
The first part of the Modem class creates an integer variable called speed. This variable is an attribute of the object; the name is one of the things that distinguishes a modem from other modems.
The second part of the Modem class is a method called displaySpeed(). This method is some of the object's behavior. It contains one statement: a System.out.println() statement that displays the modem's speed value along with some text.
If you wanted to use a Modem object in a program, you would create the object much like you would create a variable. You could use the following statement:
Modem com = new Modem();
This statement creates a Modem object called com. You can now use the object in the program; you can set its variables and call its methods. To set the value of the speed variable of the com object, you could use the following statement:
com.speed = 28800;
To make this modem display its speed by calling the displaySpeed() method,you could use the following code:
com.displaySpeed();
The Modem object named com would respond to this statement by displaying the text Speed: 28800.
Understanding Inheritance | Next Section

Account Sign In
View your cart