English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
JavaBean is a special Java class written in Java and encoded according to the JavaBeans API specification.
The following are the unique features that distinguish JavaBean from other Java classes-
It provides a default no-argument constructor.
It should be serializable and can implementSerializableinterface.
It may have many properties that can be read or written.
It may have many methods used for properties getter and setter method.
JavaBean properties are named properties that users can access on an object. The property can be any Java data type, including classes you define.
JavaBean properties can beread, write, read onlyoronly writeJavaBean properties can be accessed through two methods in the implementation class of JavaBean-
Number | Methods and Descriptions |
---|---|
1 | getPropertyName() For example, if the property name isfirstNameThen your method name will begetFirstName()to read the property. This method is called an accessor. |
2 | setPropertyName() For example, if the property name isfirstNameThen the method name will besetFirstName(),to write to the property. This method is called a mutator. |
Read-only properties will only havegetPropertyName()method, while read-only properties will only havesetPropertyName()method.
class StudentsBean implements java.io.Serializable { private String firstName = null; private String lastName = null; private int age = 0; public StudentsBean() { } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getAge() { return age; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public void setAge(Integer age) { this.age = age; } } public class Tester { public static void main(String[] args) { StudentsBean bean = new StudentsBean(); bean.setFirstName("Mahesh"); System.out.println(bean.getFirstName()); } }
Output Result
Mahesh