- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaBean class in Java
A JavaBean is a specially constructed Java class written in the Java and coded according to the JavaBeans API specifications.
Following are the unique characteristics that distinguish a JavaBean from other Java classes −
It provides a default, no-argument constructor.
It should be serializable and that which can implement the Serializable interface.
It may have a number of properties which can be read or written.
It may have a number of "getter" and "setter" methods for the properties.
JavaBeans Properties
A JavaBean property is a named attribute that can be accessed by the user of the object. The attribute can be of any Java data type, including the classes that you define.
A JavaBean property may be read, write, read only, or write only. JavaBean properties are accessed through two methods in the JavaBean's implementation class −
Sr.No. | Method & Description |
---|---|
1 | getPropertyName() For example, if property name is firstName, your method name would be getFirstName() to read that property. This method is called accessor. |
2 | setPropertyName() For example, if property name is firstName, your method name would be setFirstName() to write that property. This method is called mutator. |
A read-only attribute will have only a getPropertyName() method, and a write-only attribute will have only a setPropertyName() method.
Example
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
Mahesh
- Related Articles
- What is the class "class" in Java?
- Final class in Java
- Abstract class in Java
- Object class in Java
- StringTokenizer class in Java
- Timer Class in Java
- Static class in Java
- GregorianCalendar Class in Java
- Date Class in Java
- CopyOnWriteArraySet Class in Java
- CopyOnWriteArrayList Class in Java
- Inner class in Java
- ArrayBlockingQueue Class in Java
- LinkedBlockingQueue Class in Java
- PriorityBlockingQueue Class in Java
