What is Java Bean with Example

Java Bean : Java bean is a class which contains all the properties are private and contains getter/setter methods corresponding to that.

A public non-argument constructor is present in it.

It implements Serializable interface which have no methods defined in it.

Bean is basically used for serialization of data and maintain its state during transfer.

public class Employee implements Serializable{

   private int id;
   private String name;  
   private int salary;

   public Employee() {}

   public Employee(String name, int salary) {
      this.name = name;
      this.salary = salary;
   }
   public int getId() {
      return id;
   }
   public void setId( int id ) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName( String name ) {
      this.name = name;
   }
   public int getSalary() {
      return salary;
   }
   public void setSalary( int salary ) {
      this.salary = salary;
   }
}

Share this

Related Posts

Previous
Next Post »