Overview
JavaBeans are usual Java classes which adhere to certain coding conventions. Following
are the coding conventions I am talking about :
- Implements java.io.Serializable interface
- Provides no argument constructor
- Provides getter and setter methods for accessing it's properties
Let's now create a simple JavaBean class.
A simple JavaBean class
Create a new SimpleBean.java file and place it in the /WEB-INF/classes/com/stardeveloper/bean/test/
folder so that the complete path is :
/WEB-INF/classes/com/stardeveloper/bean/test/SimpleBean.java
Now copy the following code and paste it into the SimpleBean.java file we created
above :
package com.stardeveloper.bean.test;
public class SimpleBean implements java.io.Serializable {
/* Properties */
private String name = null;
private int age = 0;
/* Empty Constructor */
public SimpleBean() {}
/* Getter and Setter Methods */
public String getName() {
return name;
}
public void setName(String s) {
name = s;
}
public int getAge() {
return age;
}
public void setAge(int i) {
age = i;
}
}
Explanation
First line is the package statement :
package com.stardeveloper.bean.test;
Next we define our class and make it implement java.io.Serializable interface. Notice
that Serializable interface doesn't contain any method. Implementing it just flags to
the compiler that we might be serializing this class's objects.
public class SimpleBean implements java.io.Serializable {
Then we declare two variables which hold name and age of a person. These variables
inside a JavaBean are called as properties. These properties are private and are thus
not directly accessible by other classes. To make them accessible we provide getter
and setter methods to get and set their values.
private String name = null;
private int age = 0;
Next we create an empty argument constructor. Keep in mind that the only requirement
to a JavaBean is an empty "argument" constructor, not that you shouldn't use constructor
at all.
public SimpleBean() {}
Explanation
The convention for writing getter and setter methods for JavaBean's properties
is really simple. All you have to do is to take the property name e.g. name. Make it's
first character uppercase e.g. Name. Now append 'get' for getter method and 'set' for
setter method so that it becomes :
public String getName() {
return name;
}
public void setName(String s) {
name = s;
}
See! how easy it is. Since name variable is of type String, we set the return type of
getName() to String. Same is the case with setName() method which takes a parameter of
type String because name is of type String.
Next we added four getter and setter methods for private variables ( properties )
name and age.
public String getName() {
return name;
}
public void setName(String s) {
name = s;
}
public int getAge() {
return age;
}
public void setAge(int i) {
age = i;
}
Now close the class.
}
Compiling JavaBean
You will compile JavaBean like you will compile any other Java class file. After
compilation, a SimpleBean.class file will be created.
We are now done with SimpleBean.
Summary
We learned that JavaBeans are Java classes which adhere to an extremely simple
coding convention. All you have to do is to implement java.io.Serializable interface,
use a public empty argument constructor and provide public getter and setter methods to
get and set the values of private variables ( properties ).
Now move over to the next article about calling this JavaBean from within a JSP
page. That article will also explain the use of JSP <jsp:useBean>, <jsp:setProperty>
and <jsp:getProperty> tags which are provided to you by the JSP specification to make
use of JavaBeans.