Javabeans Classes
Javabeans Classes
What is JavaBeans?
-Are classes that encapsulate multiple
objects into single object.
It also helps in accessing these object
from multiple places. It contain several
elements like CONSTRUCTORS , GETTER
and SETTER methods. In Javabeans
the following conventions should be
followed.
i)CLASS DEFINITIONS: They are defined
as public classes.
ii)OPTIONAL: While not mandatory they
often implement Serializable interface to
allow object data to be converted into
stream of bytes for storage or
transmission.
iii) NO ARGUMENT
CONSTRUCTOR:
Javabeans must have a public
constructor that take no arguments.
iv) GETTER AND SETTER METHODS:
for
each private field there should be
corresponding public getter and setter
method.
-Getter methods retrieve the
value of a field.
-Setter methods modify the value
of a field.
Example of JAVABEAN
CLASS. File Name:
package mypack;
public class Student implements
java.io.Serializable{
private int id;
private String name;
public Student(){
public void set Id(int id){
this .id=id;
}
Public int getId(){
return id;
}
Public void setName(String name){
this.name=name;
}
public String getName(){
return name;}
}
HOW TO ACCESS THE
JAVABEAN CLASS
To access the javabean class we should
use the getter and setter methods
EXAMPLE:
File Name: Test.java
Package my pack;
Public class Test(
Public static void main(String[] args){
Student s=new Student();
s.setName(“TEACHER”);
System.out.println(e.getName());
}}
PROPERTIES OF SETTER METHODS
1.it must be public in nature
2.The return type should be void
3.it should take some argument
PROPERTIES OF GETTER
METHOD
1. It should be public in nature
2.The return type should not be void
3.it does not take any argument