What is a Java bean?

Ryan Glen picture Ryan Glen · Jul 10, 2012 · Viewed 69.2k times · Source

Possible Duplicate:
What's the point of beans?

What is a javabean? What is it used for? And what are some code examples? I heard it is used for something to do with getter and setter methods? I'm quite confused about what a java bean is and where you even access it. I googled it but couldn't find a definite answer.

Answer

Pramod Kumar picture Pramod Kumar · Jul 10, 2012

Java Bean is a normal Java class which has private properties with its public getter and setter method.

Java Beans are generally used as helper class.

Example -

public class User 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;
    }
}

Implementing Serializable is not mandatory but is very useful if you'd like to persist or transfer Javabeans outside Java's memory, e.g. in harddisk or over network.

Places where JavaBeans are used?