Sams Teach Yourself Java 2 in 24 Hours

Sams Teach Yourself Java 2 in 24 Hours

By Rogers Cadenhead

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:

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.

12fig02.gif

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.

12fig03.gif

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."

Share ThisShare This

Informit Network