Most of the beginners to Java who have limited exposure to the language are asked this question to check their understanding concepts. Note that both Java bean and Enterprise Java bean are completely different and have nothing in common as far as their purpose is concerned.

Java Bean

A Java bean is a class with following properties:

  • The class should be public.
  • The member variables of the class should be accessible using getXYZ, setXYZ, isXYZ and other modifier methods. The isXYZ member methods are used for checking the value of a member variable of type boolean.
  • The class should have a public no-arg constructor. This is required because a Java bean’s member variables should be initialized using the setXYZ methods.
  • The class should be Serializable or Externalizable. This is done so as to persist the state of the bean member variables and restore the state when required.

A sample Java bean looks like:

package com.example;

import java.io.Serializable;

public class Bar implements Serializable {
 
    private String name = null;
 
    private boolean flag = false;
 
    /** public No-arg constructor */
    public Bar() {
    }
 
    /**
     * Property <code>name</code> (note capitalization) readable/writable.
     */
    public String getName() {
        return this.name;
    }
 
    /**
     * Setter for property <code>name</code>.
     */
    public void setName(final String name) {
        this.name = name;
    }
 
    /**
     * Getter for property "flag"
     * Different syntax for a boolean field (is vs. get)
     */
    public boolean isFlag() {
        return this.flag;
    }
 
    /**
     * Setter for property <code>flag</code>.
     */
    public void setFlag(final boolean flag) {
        this.flag = flag;
    }
}

Enterprise Java Bean (EJB)

An Enterprise Java Bean is a class which runs on a Java EE application server and is used for encapsulating the business logic. There are multiple types of Enterprise Beans which include:

  • Stateful
  • Stateless
  • Entity (deprecated after the onset of JPA)
  • Message Driven Beans

A basic stateless EJB will look like:

@Stateless 
public class EmployeeServiceBean {
    @PersistenceContext 
    EntityManager em;

    public void addEmployee(Employee emp) { 
        em.persist(emp); 
    } 
}

With EJB2.x, the EJB classes were required to implement EJBobject interface and specify the type of EJB in deployment descriptor but with EJB3.x, annotations have been introduced which make coding simpler for the developer.

Plain Old Java Object (POJO)

A POJO is a simple class with no special requirements like implementing an interface or extending from a class. A POJO doesn’t have any guidelines associated with it. The most distinguishing factor to define a POJO class is that it doesn’t have anything to do with Java EE Enterprise Java beans. Thus we can say that a POJO is not necessarily a Java bean but a Java bean is always a POJO.

Related Posts

What is the difference between POJO,JavaBean and EJB admin Core Java
Most of the beginners to Java who have limited exposure to the language are asked this question to check their understanding concepts. Note that both Java bean and Enterprise Java bean are completely different and have nothing in common as far as their purpose is concerned. Java Bean A Java bean...
Most of the beginners to Java who have limited exposure to the language are asked this question to check their understanding concepts. Note that both Java bean and Enterprise Java bean are completely different and have nothing in common as far as their purpose is concerned. <h2>Java Bean</h2> A Java bean is a class with following properties: <ul> <li>The class should be public.</li> <li>The member variables of the class should be accessible using getXYZ, setXYZ, isXYZ and other modifier methods. The isXYZ member methods are used for checking the value of a member variable of type boolean.</li> <li>The class should have a public no-arg constructor. This is required because a Java bean's member variables should be initialized using the setXYZ methods.</li> <li>The class should be <a href="http://www.javaexperience.com/java-externalizable-vs-serializable-interfaces/" title="Externalizable vs Serializable Interfaces">Serializable or Externalizable</a>. This is done so as to persist the state of the bean member variables and restore the state when required.</li> </ul> A sample Java bean looks like: 1 <h2>Enterprise Java Bean (EJB)</h2> An Enterprise Java Bean is a class which runs on a Java EE application server and is used for encapsulating the business logic. There are multiple types of Enterprise Beans which include: <ul> <li>Stateful</li> <li>Stateless</li> <li>Entity (deprecated after the onset of JPA)</li> <li>Message Driven Beans</li> </ul> A basic stateless EJB will look like: 1 With EJB2.x, the EJB classes were required to implement EJBobject interface and specify the type of EJB in deployment descriptor but with EJB3.x, annotations have been introduced which make coding simpler for the developer. <h2>Plain Old Java Object (POJO)</h2> A POJO is a simple class with no special requirements like implementing an interface or extending from a class. A POJO doesn't have any guidelines associated with it. The most distinguishing factor to define a POJO class is that it doesn't have anything to do with Java EE Enterprise Java beans. Thus we can say that a POJO is not necessarily a Java bean but a Java bean is always a POJO.
Multiple inheritance in Java
What is the difference between JAR,WAR and EAR files
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Comments

comments