Java - Understanding beans
by
Jeremy Canfield |
Updated: September 16 2022
| Java articles
A bean is simply a class that adheres to the following rules.
- Implements java.io.Serializable
- A public no-argument constructor
- Has getters and setters methods
Let's say you've created a class called Hello.java (in Eclipse in this example).
This bean will print "John Doe".
package my.pkg;
public class Hello implements java.io.Serializable {
private static final long serialVersionUID = -3774654564564563L;
private String name;
public void myName() {
name = "John Doe";
}
// getter
public String getName() {
return name;
}
// setter
public void setName(String name) {
this.name = name;
}
}
Did you find this article helpful?
If so, consider buying me a coffee over at