In the previous example, the Person
class defined properties name
and age
, of types String
and int
respectively. The properties were defined by the fact that the class defined methods getName
, setName
, getAge
, and setAge
.
The JavaBeans specification details how to define properties; however, the basic rules are as follows:
To define a configurable property, a class defines a
getX
method that takes no arguments and returns a value, and asetX
method that takes one argument and returnsvoid
. The type returned by thegetX
method must be the exact same type as the type taken as an argument by thesetX
method, and can be any Java type. Both thegetX
andsetX
methods must be declaredpublic
.One exception applies: the
getX
method for a Boolean property can be replaced byisX
. For example, the Boolean propertyrunning
can be defined by the methodgetRunning()
orisRunning()
.The property name is formed by removing
get
orset
from the method name and changing the first letter of the remaining string to lower case. For example, the methodgetFirstName()
defines a property calledfirstName
.One exception applies: if the first two letters of the remaining string are both capitalized, no letters are changed to lower case. For example, the method
getURL()
defines a property calledURL
.
Property names are case-sensitive. Thus, the entry Age=20
does not set the property Person.age
.