Bean Internals  «Prev  Next»
Lesson 10Indexed property accessor methods
ObjectiveLearn about indexed property accessor methods.

JavaBeans Indexed Property Accessor Methods

Just like all Bean properties, indexed properties are accessed through accessor methods.
However, indexed properties often support an additional pair of accessor methods since it is useful to be able to access either an individual element or the entire array of elements. So, indexed properties have two pairs of accessor methods
  1. one pair gets and sets individual properties in the property array via an index,
  2. The other pair gets and sets the entire array of properties as a single entity.

Implementing Accessor Methods

Just as with single-valued (non-indexed) properties, you are free to implement any of these accessor methods you choose.
For example, you may not want to allow direct access to the entire array of properties, in which case you would not implement the second pair of accessor methods. Optionally, you could make an indexed property read-only by eliminating both setter methods.
  • Persistent values should be accessed through methods called getXxx and setXxx. For example, if your Car class stores the current number of passengers, you might have methods named getNumPassengers (which takes no arguments and returns an int) and setNumPassengers (which takes an int and has a void return type). In such a case, the Car class is said to have a property named numPassengers (notice the lowercase n in the property name, but the uppercase N in the method names). If the class has a getXxx method but no corresponding setXxx, the class is said to have a read-only property named xxx.
    The one exception to this naming convention is with boolean properties: they are permitted to use a method called isXxx to look up their values. So, for example, your Car class might have methods called isLeased (which takes no arguments and returns a boolean) and setLeased (which takes a boolean and has a void return type), and would be said to have a boolean property named leased (again, notice the lowercase leading letter in the property name).



Following are the accessor methods for an indexed property of type Colornamed palette:
publicColor getPalette(
intindex);
public voidsetPalette(int index, Color color);
publicColor[] getPalette();
public voidsetPalette(Color[] colors);

The first two accessor methods for the palette indexed property get and set an individual element from the property array, which is evident by the integer index required by each method.
The last two methods get and set the palette property array as a whole, which is evident by the array brackets
([]) 
used when defining the property type.
These last two methods are pretty powerful since they enable you to get and set the entire property array at once.

System that tracks Stock Prices

Imagine that we are building a system that tracks stock prices in real time. First, let us think about building a watch list object that keeps track of prices for a set of stocks. We will call this class a WatchList. We then define a property of the WatchList called Stocks which contains a list of stocks for which prices are being tracked. The type of the Stocks property is a String array, and each individual stock is represented by a String. We also implement a read-only property called StockCount that returns the number of stocks in the WatchList. The code looks like this:

import java.util.*;
public class WatchList{
  // a vector that contains the actual stock names
  protected Vector stocks = new Vector();
  // constructor
  public WatchList(){}
   // the get method for the StockCount property
   public synchronized int getStockCount(){ 
    // the StockCount property is derived from the size of the
    // stocks Vector
    return stocks.size();
  }
  // get method for Stocks property array
  public synchronized String[] getStocks(){
   // we don't currently deal with the case where the watch list is empty
   // allocate an array of strings for the stock names
   String[] s = new String[getStockCount()];
   // copy the elements of the stocks Vector into the string array,
   // and then return the array
   stocks.copyInto(s);
  return s;
 }


 // set method for Stocks property array
 public synchronized void setStocks(String[] s){
  // the existing list of stocks is removed in favor of the
  // new set of stocks
  // set the size of the stocks vector to match the length
  // of the new array
  stocks.setSize(s.length);
  // copy the values into the stocks vector
  for (int i = 0; i < s.length; i++){
   // use the single stock set method
   try{
    setStocks(i, s[i]);
   }
   catch (ArrayIndexOutOfBoundsException e){
   //
  }
 }
}

 // get method for single element of Stocks property
 public synchronized String getStocks(int index)
  throws ArrayIndexOutOfBoundsException
  {
  // make sure the index is in bounds
  if (index < 0 || index >= getStockCount()){
   throw new ArrayIndexOutOfBoundsException();
  }
  // get the stock and return it
  String s = (String)stocks.elementAt(index);
  return s;
 }

 // set an individual element of the Stocks property array
 public synchronized void setStocks(int index, String stock)
  throws ArrayIndexOutOfBoundsException
  {
  // make sure the index is in bounds
  if (index < 0 || index >= getStockCount()){
   throw new ArrayIndexOutOfBoundsException();
  }
  // change the stock at the specified index
  stocks.setElementAt(stock, index);
 }
}

Indexed Property - Exercise

Click the Exercise link below to define the accessor methods for an indexed property.
Indexed Property - Exercise
In the next lesson, bound properties will be discussed.

SEMrush Software