18

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 :-)

OrangeDog
  • 36,653
  • 12
  • 122
  • 207

4 Answers4

10

A javabean is a standard. All Javabeans have the following 3 qualities:

1) The class implements Serializable
2) All fields have public setters and getters to control access.
3) A public no-argument constructor.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
  • 6
    I thought serializable was not necessary. – assylias Aug 06 '12 at 13:46
  • i think it is necessary to meet the standard; in practice might not be necessary to function in all cases. – hvgotcodes Aug 06 '12 at 13:50
  • @assylias I'm not sure. But in [Android](http://www.android.com), `Serializable` or [`Parcelable`](http://developer.android.com/reference/android/os/Parcelable.html) is important to beans, at least for me. Data can be only transferred between activities/ services… if it implements one of the both. – SheIs_LeThiCongNhan Aug 06 '12 at 13:54
  • The standard doesn't appear to define a Java Bean at all, although it does make a claim that it does. Very confused. There doesn't appear to be any element that is mandatory. Indeed a Java Bean can be a serialised file and not a class as such. – Tom Hawtin - tackline Aug 06 '12 at 13:55
3

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
  • boolean getters use the prefix 'is'

And I think strictly it's the instances that are "beans", not the class.

OrangeDog
  • 36,653
  • 12
  • 122
  • 207
2

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).

Jason Huntley
  • 3,827
  • 2
  • 20
  • 27
0

Complementing the answers of our fellows:

  1. Add a listener with an addXXXListener method.
  2. Remove a listener with a removeXXXListener method.
  3. boolean (primitive) fields should have an isXXX method instead of a getXXX method.

As it is a standard, it is important to follow it, since may libraries and technologies in Java will use it under the hood. Examples: Expression language in JSPs, GUI builders, etc.

Specification: http://download.oracle.com/otndocs/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/

davidbuzatto
  • 9,207
  • 1
  • 43
  • 50
  • They are not necessary, but if you have to use, you should follow the standard. At least I studied it when I got my SCJP five years ago. Take a look: http://www.oracle.com/technetwork/java/javase/documentation/javabeans-getlisteners-192680.html – davidbuzatto Aug 06 '12 at 13:53