Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Java Bean Class


A Java Bean is a java class that should follow following conventions:

  • It should have a no-arg constructor.
  • It should be Serializable.
  • It should provide methods to set and get the values of the properties, known as getter and setter methods.

Why use Java Bean?

According to Java white paper, it is a reusable software component. A bean encapsulates many objects into one object, so we can access this object from multiple places. Moreover, it provides the easy maintenance.

Simple example of java bean class

  1. //Employee.java
  2. package mypack;
  3. public class Employee implements java.io.Serializable{
  4. private int id;
  5. private String name;
  6. public Employee(){}
  7. public void setId(int id){this.id=id;}
  8. public int getId(){return id;}
  9. public void setName(String name){this.name=name;}
  10. public String getName(){return name;}
  11. }

How to access the java bean class?

To access the java bean class, we should use getter and setter methods.
  1. package mypack;
  2. public class Test{
  3. public static void main(String args[]){
  4. Employee e=new Employee();//object is created
  5. e.setName(“Arjun”);//setting value to the object
  6. System.out.println(e.getName());
  7. }}

Note: There are two ways to provide values to the object, one way is by constructor and second is by setter method.

Comments

comments