A JavaBean is nothing more than a class file that follows some basic rules of design. As a rule, beans are component classes, rather than applications or applets.The basic rules of design are:
- All properties are declared as private, class-level variables.
- To make a property readable, define an accessor (getter) method that returns the value of the property.
- declare the method public
- declare the return type to be the same as the property variable
- begin the method name with the word 'get'
- finish the method name using the name of the variable, capitalizing the first letter
- the method takes no argument (exception to this rule)
- To make a property writable, define a mutator (setter) method that accepts a new value for the property.
- declare the method public
- declare the return type to be void
- begin the method name with the word 'set'
- finish the method name using the name of the variable, capitalizing the first letter
- the method takes one argument of the same type as the property variable (exception to this rule)
The exception to rules 2.5 and 3.5 is if the property is indexed. A property would be indexed if the property were an array or other type of list. In this case, the accessor and mutator methods would take an additional, numeric argument for the index to the value in the list.
Additionally, properties may be bound or constrained.
A bound property is simply one that sends out a notification when it has changed. The bean maintains a list of other objects that are listening for the change notification. The other objects are added as PropertyChangeListeners. This works in a similar manner as the AWT listeners we've already used. For example, the ActionListener:
- An application may implement ActionListener. Because it does, it is required to have the actionPerformed() method.
- A button on the application may register the application as an ActionListener: btnExit.addActionListener(this).
- When the button is clicked, it goes through it's list of ActionListeners and calls their actionPerformed() method.
A constrained property is one that has an acceptable range of values. When the mutator method of the property is called, the property has the right to veto the change if the new value does not meet the defined contraints.
Refer to the CIS133 Lecture.An additional attribute to the <applet> tag is archive. Often, an Applet may require additional supporting files. The value of the archive may be a comma-separated list of class files needed by the Applet. Another option is to place all the support files into a JAR file.
The JAR utility works in the same manner as a ZIP utility. It combines and optionally compresses files into a single file with a "jar' extension.
The simplest ways to use the JAR utility are:
Extracting:
jar xfv jar-file-name
This extracts all files from the named JAR file. Make sure you are in the folder containing the JAR file.
The "x" means you are extracting.
The "f" means you are naming the JAR file.
The "v" means you want verbose output as the utility is processing.Creating:
jar cfv jar-file-name files
This creates the named jar file from the list of files you pass.
The "c" means you are creating.
Files are a space-separated list of files and/or folders.