Saturday, 6 April 2013

JavaBeans Standards

JavaBean Property Naming Rules

  • If the property is not a boolean, the getter method's prefix must be get. For example, getSize()is a valid JavaBeans getter name for a property named "size." Keep in mind that you do not need to have a variable named size (although some IDEs expect it).
The name of the property is inferred from the getters and setters, not through any variables in your class. What you return from getSize() is up to you.
  • If the property is a boolean, the getter method's prefix is either get or is. For example, getStopped() or isStopped() are both valid JavaBeans names for a boolean property.
  • The setter method's prefix must be set. For example, setSize() is the valid JavaBean name for a property named size.
  • To complete the name of a getter or setter method, change the first letter of the property name to uppercase, and then append it to the appropriate prefix (
    get, is, or set).
  • Setter method signatures must be marked
    public, with a void return type and an argument that represents the property type.
  • Getter method signatures must be marked
    public, take no arguments, and have a return type that matches the argument type of the setter method for that property.
  • JavaBean Listener Naming Rules

    • Listener method names used to "register" a listener with an event source must use the prefix
      add, followed by the listener type. For example, addActionListener() is a valid name for a method that an event source will have to allow others to register for Action events.
  • Listener method names used to remove ("unregister") a listener must use the prefix
    remove, followed by the listener type (using the same rules as the registration add method).
  • The type of listener to be added or removed must be passed as the argument to the method.
  • Listener method names must end with the word "Listener".
  • Examples of valid JavaBean method signatures are
    public void setMyValue(int v)
    public int getMyValue()
    public boolean isMyStatus()
    public void addMyListener(MyListener m)
    public void removeMyListener(MyListener m)

    Examples of invalid JavaBean method signatures are
    void setCustomerName(String s) // must be public
    public void modifyMyValue(int v) // can't use 'modify'
    public void addXListener(MyListener m) // listener type mismatch