tutorial - purpose of java beans
What are JavaBeans in plain English? (3)
Is that it? Am I close?
Yes, you are relatively correct. Most beans adhere to such basic rules for definition. However, just a few more things to add. To distinguish beans from POJO (Plain Old Java Object), beans have a default constructor and usually implement the serializable interface.
This allows you to work with basic models across many frameworks. Beans are mostly used for storing and retrieving data in a simple layout structure so data models can be shared throughout specific architectures. Examples include firing events in a UI using the same data for working with different dialogs and or retrieving results for a given ORM (Object Relationship Mappings). Additional examples you may want to look at are DTO (Data Transfer Object), VO (Value Objects), and EJBs (Enterprise Java Beans).
Before I start I would just like everyone know that I did indeed spend a good time googling this and found a lot of explanations and definitions. But even so after spending hours reading the subject still seems rather vague. I know I have to ask questions that can better the community but this one is just for me to see if I have a clear understanding of JavaBeans.
From what I can make out, a JavaBean is basically a class just like any other java class except that it adheres to certain conventions, i.e.:
- The class must implement Serializeable
- Class properties are assumed to be private and their names start with a lowercase letter
- Each property must have it's respective getter and setter methods.
- Each setter method starts with the prefix 'get' followed by the property name e.g. setName()
- Setter methods are public and void
- Same applies to the getter methods (prefix 'get', public, return type respective property class type etc.)
- For boolean properties instead of 'get' one uses the prefix 'is'
- Strictly speaking it is the instance of the class that is considered a 'bean' not the class itself.
And there you have it, after a very long time of reading, that's what I can make out... Is that it? Am I close? Do I have this completely wrong?
...Thanks for everyone's answers so that I could update this bullet list :-)
A javabean is a standard. All Javabeans have the following 3 qualities:
1) The class implements
2) All fields have public setters and getters to control access.
3) A public no-argument constructor.
Yep, that's pretty much it.
Just a couple of extra bits:
- Getters take no parameters, and setters take a single parameter of the same type as the property
- Properties can be read- or write-only by omitting the setter or getter respectively
booleangetters use the prefix 'is'
And I think strictly it's the instances that are "beans", not the class.