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
- The Power of Inheritance
- Establishing Inheritance
- Working with Existing Objects
- Workshop: Creating a Subclass
- Summary
- Q&A
- Quiz
- Activities
- 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
Working with Existing Objects
One of the things you have learned about object-oriented programming is how it encourages reuse. If you develop an excellent spell-checking object for use with one Java programming project, it should be possible to incorporate that object into another project without modification.
If a Java class is well-designed, it's possible to make that class available for use in other programs. As long as the class is documented thoroughly, a programmer should be able to work with that class as if it were part of the official Java language itself.
This is an idea that has great potential for software developers. The more objects available for use in your programs, the less work you have to do when creating your own software. If there's an excellent spell-checking object that suits your needs, you can use it instead of writing your own. Ideally, you can even give your boss a false impression about how long it took to add spell-checking functionality to your project, and use this extra time to make personal long-distance calls from the office.
When Java was first introduced, the system of sharing objects was largely an informal one. Programmers developed their objects to be as independent as possible and protected them against misuse through the use of private variables and public methods to read and write those variables.
Sharing objects becomes more powerful when there's a standard for developing reusable objects. The benefits of a standard include the following:
- There's less need to document how an object works, because anyone who knows the standard already knows a lot about how it functions.
- Development tools can be designed that follow the standard, making it possible to work more easily with these objects.
- Two objects that follow the standard will be able to interact with each other without special programming to make them compatible.
The standard for developing reusable objects in Java is called JavaBeans, and each individual object is called a Bean.
Developing JavaBeans
JavaBeans are Java classes designed specifically for the purpose of being reused. These reusable classes, which are called software components in many programming languages, are developed under a standard set of rules.
Developing Beans requires a programming tool in addition to the Software Development Kit. Sun Microsystems offers two tools that demonstrate the technology, the Bean Builder and JavaBeans Development Kit,at http://java.sun.com/beans/software.
There are also several programs that make it possible to develop Beans and incorporate them into programs, including Borland JBuilder, NetBeans, Sun ONE Studio, and IBM VisualAge for Java.
The JavaBeans Development Kit, also called the BDK, includes the BeanBox, a visual tool that is used to add Beans to a Java program and make those Beans work with each other. The BeanBox, which is a Java program itself, is shown in Figure 12.2.
Figure 12.2 Using the BeanBox to create a Java program out of several Beans.
In Figure 12.2, the BeanBox is being used to link together three Beans: Two buttons and an animated Bean that displays the Java mascot, an anthropomorphic cuspid named Duke, juggling some giant beans. A lot of JavaBeans programming can be accomplished by using a tool like the BeanBox—some projects can be accomplished entirely with the mouse and no coding of your own classes.
Bean programming is a specialized aspect of Java that's best learned after you have mastered the basics of the language. However, a beginning programmer can accomplish a lot by working with existing Beans, so you'll benefit by becoming more familiar with JavaBeans as you learn to program.
You'll be developing skills throughout this book that are directly applicable to JavaBeans programming. A prime example is encapsulation, which you learned about during the previous hour. Using methods to read and write the values of a variable is a fundamental part of JavaBeans development.
During the previous hour, you saw how getSeconds() and setSeconds() methods could be used in a class to read and write a variable called newSeconds:
public int getSeconds() {
return newSeconds;
}
public void setSeconds(int newValue) {
if (newValue > 60)
newSeconds = newValue;
}
If the newSeconds variable is private, these methods are an example of encapsulation. One of the rules of JavaBeans programming is to limit access to object variables according to the following rules:
- The variable should be private.
- A public method to read the variable should begin with get, end with a name that describes the variable being read, and return the same type of data as the variable.
- A public method to write the variable should begin with set, end with a name that describes the variable being written, and return void.
- If there are both reading and writing methods, they should end with the same name to describe the variable.
The preceding example follows all of these rules with the getSeconds and setSeconds methods. Because of this, if these methods are part of a Bean, the newSeconds variable can be manipulated directly within a tool like the BeanBox.
In Figure 12.3, the Properties window shows a seconds property that can be changed from within the BeanBox. This shows up because a setSeconds() method exists in a Bean that's selected in the main BeanBox window.
Figure 12.3 Manipulating a Bean's variables with the BeanBox's Properties window.
Much of the work that's done creating JavaBeans is for the benefit of programmers using a development environment such as the BeanBox. The more a Bean can be customized, the more useful it is.
Following the set and get rules for using an object variable is a first step towards making a class a Bean, and they demonstrate the kind of programming that JavaBeans requires.
These rules make sense for all object-oriented programming that you do because they present a useful and safe interface between an object and the other classes that will use it. You'll learn more skills that are useful in Bean development during Hour 22, "Playing Sound Files."
Workshop: Creating a Subclass | Next Section

Account Sign In
View your cart