Difference between POJO and Beans in JAVA

Newbie Software Engineer
2 min readJan 22, 2022

--

source: https://unsplash.com/@markdaynes

Update: This account has been merged with this account. Please follow the new account to get latest articles.

POJO: Plain Old Java Object

A fancy term coined by Martin Fowler in 2000.

Any java class is a POJO if:
1. it doesn’t extend any other java class
2. it doesn’t implement any interface
3. it doesn’t use any outside annotation

Example:
This is a POJO class.

public class Animal {
String name;
int age;
}

The below class is not a POJO class.

public class Horse extends Animal {
String name;
int age;
}

Neither is this one

@Entity
public class Animal {
String name;
int age;
}

Java Beans:

A java class needs to have all the below if be declared as Java Bean:

  1. No-args constructor, which is there automatically if no other constructor is declared
  2. All the properties must be private
  3. There should be public getters and setters
  4. Must be serializable

Example of Java Bean:

import java.io.Serializable;
public class Horse implements Serializable {
private String name;
private int age;
// no-arg constructor is already available public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
}

By implementing the Serializable interface, we made this class serializable.
This class has private properties and public getters and setters.

Happy Learning :)

--

--