How do I define a JavaBeans property?

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;
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact