How do I define a JavaBeans property?
Created Feb 12, 2000
John Zukowski Properties are defined/identified/accessed by a pair of get / set methods:
Read-only properties only have a get method.
- public void setXXX(TYPE value);
- public TYPE getXXX();
For example:
public void setMood(int mood) {
this.mood = mood;
repaint();
}
public int getMood() {
return mood;
}
This naming pattern is used so that properties can be identified at runtime by other programs (such as GUI builders), though is not a strict requirement as you can provide a BeanInfo class for the component that identifies other methods to be used.
Write-only properties only have a set method.
Also, boolean-valued properties can use the get / is method pattern
public boolean isXXX();
For example:
public boolean isRunning() {
return state;
}