What are JavaBeans?

evertwagenaar.com logo

What are JavaBeans?

According to Wikipedia:

JavaBeans are classes that encapsulate many objects into a single object (the bean). They are serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods. The name “Bean” was given to encompass this standard, which aims to create reusable software components for Java.

According to Evert:

A JavaBean is a normal class with a few properties:

  • Javabeans have a zero argument Constructor. They cannot be instantiated with Data. The data is always added after the instantiation.
  • JavaBeans implement the Serializable interface which make them serializable.
  • All class-members are private.
  • To access data inside the class you need to create so-called ‘Accessor methods’ these are the getters and setters. You can only change the values of the class members by calling setProperty(). The same thing is true to get read the private class-members. This is done by the getters. getProperty().
  • Accessor Methods have a naming convention as follows: Lowecas get or set, followed by the property name (capitalized) so if the bean has a property called id, it’s accessor-methods become getId() and setId().
  • JavaBeans can be serialized.
  • Javabeans know things about theirselves. This is called Introspection.
So what are they used for?

JavaBeans are typically used as data-carriers, for example a JavaBean can be used to represent a Database Table Row, which makes sense in a DAO (Data Access  Object). This is indeed one of the most common applications of JavaBeans.

Utilities
Example of Introspection.

As I already mentioned, JavaBeans can look inside themselves, for example to get the class name:

// Get the fully-qualified name of a class
Class cls = java.lang.String.class;
String name = cls.getName();        // java.lang.String

// Get the fully-qualified name of a inner class
cls = java.util.Map.Entry.class;
name = cls.getName();               // java.util.Map$Entry

// Get the unqualified name of a class
cls = java.util.Map.Entry.class;
name = cls.getName();
if (name.lastIndexOf('.') > 0) {
    name = name.substring(name.lastIndexOf('.')+1);  // Map$Entry
}
// The $ can be converted to a .
name = name.replace('$', '.');      // Map.Entry


// Get the name of a primitive type
name = int.class.getName();         // int

// Get the name of an array
name = boolean[].class.getName();   // [Z
name = byte[].class.getName();      // [B
name = char[].class.getName();      // [C
name = short[].class.getName();     // [S
name = int[].class.getName();       // [I
name = long[].class.getName();      // [J
name = float[].class.getName();     // [F
name = double[].class.getName();    // [D
name = String[].class.getName();    // [Ljava.lang.String;
name = int[][].class.getName();     // [[I

// Get the name of void
cls = Void.TYPE;
name = cls.getName();               // void

A more complex example of Introspection can be found here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.