JavaBean Standards in Java
Today lets talk about Javabean Standards. JavaBeans are Java classes that have properties. For our purposes, think of properties as private instance variables. Since they’re private, the only way they can be accessed from outside of their class is through methods in the class. The methods that change a property’s value are called setter methods, and the methods that retrieve a property’s value are called getter methods.
JavaBean Standards in Java and 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. 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.
Examples of JavaBean Standards
public void setMyValue(int v)
public int getMyValue()
public boolean isMyStatus()
Enjoy Reading This Article?
Here are some more articles you might like to read next: