Java Bean Class

Java Bean Class

  • Designing a java class with private data member,public constructor,and
    public getter and setter method is known as java bean class
  •  Java bean class pattern
    — private data member
    — public constructor
    — public getter and setter method
  •  The getter method is used to read access and the setter method is used
    to provide write access.
  •  Java bean class is good example of encapsulation.
  • It is used to built data access object and data transfer object.
Java Bean class Example
class Employee
{

	private int id;
	private String name;
	private double salary;
	
	
	public Employee(int id,String name,double salary)
	{
		
		this.id=id;
		this.name=name;
		this.salary=salary;
	
	}
	

	//public getter and setter method
	
	public int getid()
	{
		return id;
	}
	
	public void setid(int id)
	{
		this.id=id;
	}

	
	public String getname()
	{
		return "bhavesh";
	}
	
	
	public void setname(String name)
	{
		this.name=name;
	}
	
	
	public double getsalary()
	{
		return salary ;
	}
	
	public void setsalary(double salary)
	{
		this.salary=salary;
	}
	
}




public class JavaBeanclass
{

	public static void main(String[] args) {
		
		System.out.println("***************************");
		
		
		Employee emp=new Employee(1, "bhavesh", 23.9);
		
		System.out.println("Employee id: "+emp.getid());
		System.out.println("Employee name: "+emp.getname());
		System.out.println("Employee salary: "+emp.getsalary());
	
		
		System.out.println("***************************");
		
		emp.setsalary(2344.23);
		
		System.out.println("Employee Salry: "+emp.getsalary());
	}
	
}

output:
***************************
Employee id: 1
Employee name: bhavesh
Employee salary: 23.9
***************************

Leave a Reply