What is a JavaBean exactly?



A JavaBean is a Java class that satisfies certain programming conventions:

  1. The JavaBean class must implement either Serializable or Externalizable.
  2. The JavaBean class must have a no-arg constructor.
  3. All JavaBean properties must have public setter and getter methods.
  4. All JavaBean instance variables should be private.


package com.interviewbubble;public class PersonBean implements java.io.Serializable {    private boolean deceased;    private List list;    private String name;    public PersonBean() {    }    public List getList() {        return list;    }    public void setList(List list) {        this.list = list;    }    public String getName() {        return name;    }    public void setName(String value) {        this.name = value;    }    public boolean isDeceased() {        return deceased;    }    public void setDeceased(boolean value) {        deceased = value;    }}

Also, there is no syntactic difference between a JavaBean and another class — a class called a JavaBean if it follows the standards.

There is a term for it because the standard allows libraries to programmatically do things with class instances you define in a predefined way. For example, if a library wants to stream any object you pass into it, it knows it can because your object is serializable (assuming the lib requires your objects be proper JavaBeans).