A JavaBean is a Java class that has to comply with the subsequent conventions:
According to the Java white paper, it's miles a reusable software program component. A bean encapsulates many gadgets into one object in order that we are able to get entry to this object from more than one place. Moreover, it gives smooth maintenance.
//Employee.java
package mypack; public class Employee implements java.io.Serializable{ private int id; private String name; public Employee(){} public void setId(int id){this.id=id;} public int getId(){return id;} public void setName(String name){this.name=name;} public String getName(){return name;} }
We have to make use of getter and setter methods, to access the java bean class:
package mypack; public class Test{ public static void main(String args[]){ Employee e=new Employee();//object is created e.setName("Arjun");//setting value to the object System.out.println(e.getName()); }}
Note: Two ways are there to give values to the object. The first way is by making use of the constructor and the second way is by making use of the setter method.
A JavaBean property is an identified attribute that will be retrieved by the user of the Component. The attribute might be of any Java data type, having the classes that you explain.
A JavaBean property can be examined, write, examine-only, or write-only. JavaBean functions are accessed through techniques withinside the JavaBean's implementation class:
For example, if the property call is firstName, the technique call could be getFirstName() to examine that belonging. This technique is referred to as the accessor.
For example, if the property call is firstName, the technique call could be setFirstName() to jot down those belongings. This approach is known as the mutator.
Benefits of JavaBean:/p> are given below:
Disadvantages of JavaBean
Negative aspects of JavaBean are given below: