Java – programming the difference between POJOs and beans

I have the following two classes Can I say that the first is a POJO class and the second is a bean class?

1) POJO class, because it has only getter and setter methods, and all members are declared 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 member variables are private, have getters and setters, and implement the 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 parameterless constructor

Solution

The only difference is that beans can be serialized

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

Implement Java io. The serializable interface class enables serializability of the class Classes that do not implement this interface will not serialize or deserialize any of their states All subtypes of a serializable class are themselves serializable The serialization interface has no methods or fields and is only used to identify serializable semantics

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>