Bean Internals  «Prev  Next»
Lesson 16Creating properties
ObjectiveLearn how to create a property.

Create JavaBean Properties

Properties are created just like normal Java member variables.
The only thing that really distinguishes a property from a private member variable is that properties have at least one accessor method. Following is the declaration of an integer property named count, along with its accessor methods:

Analyze the following uploaded image. Print off the text and describe the features.
 Integer Count Property Declaration
1)Integer Count Property Declaration

 getCount() getter method definition
2)getCount() getter method definition

 setCount() setter method definition
3)setCount() setter method definition




Create Simple Properties

To add simple properties to a bean, add appropriate getXXX and setXXX methods (or isXXX and setXXX methods for a boolean property). The names of these methods follow specific rules called design patterns. These design pattern-based method names allow builder tools such as the NetBeans GUI Builder, to provide the following features:
  1. Discover a bean's properties
  2. Determine the properties' read/write attributes
  3. Determine the properties' types
  4. Locate the appropriate property editor for each property type
  5. Display the properties (usually in the Properties window)
  6. Alter the properties (at design time)

  • Integer property named count:
    The following section discusses the Integer property named count with its accessor methods
    1: private intcount;
    2:
    3:
    public intgetCount() {
    4:  return count;
    5:}
    6:
    7:
    public voidsetCount(int c){
    8:  count = c;
    9:}
    

    1. Line 1: Integer count property declaration
    2. Lines 3-5: getCount() getter method definition
    3. Lines 7-9: setCount() setter method definition

    In the next lesson, usage of a bean property will be discussed.

SEMrush Software