How do I define a JavaBeans property?
Created May 4, 2012
John Zukowski Properties are defined/identified/accessed by a pair of get / set methods:
- 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.
Read-only properties only have a get method.
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; }