Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming difference between POJO and Bean

I have the following two classes. Can I say the first one is a POJO class and the second one as a Bean class?

1) POJO class, since it has only getter and setter method, and all the member are declared as private

public class POJO {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setId() {
        this.id = id;
    }

    public void setName() {
        this.name = name;
    }
}

2) Bean class - all the member variables are private, has getters and setters and implements Serializable interface

public class Bean implements java.io.Serializable {
    private String name;
    private Integer age;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return this.age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

It also has a no-arg constructor.

like image 535
Vishnu Avatar asked Jan 31 '26 08:01

Vishnu


1 Answers

Only difference is bean can be serialized.

From Java docs - http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

like image 115
Ninad Pingale Avatar answered Feb 01 '26 21:02

Ninad Pingale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!