Java Beans
It is a Java class, It follows conventions they are:
- It must have a no-arg constructor
- It must be serializable.
- It must provide the methods to get and set the properties
Uses Of Java Bean:
Java bean class follows the encapsulation phenomena of object-oriented programming. Java Bean is also known as a Reusable Software component.
It encapsulates many objects into one object. So that, we can access this one object in multiple ways.
Encapsulation:
It is defined as the binding of data into a single unit known as Encapsulation.
It can also hide irrelevant data, using the concept of abstraction and Data hiding.
Uses Of Encapsulation:
- Flexibility increases
- Reusability of code
- Hiding the data
- Testing the code in an easy way
BeanExample.java
class Area {
int len;
int bre;
Area (int len, int bre) {
this.len = len;
this.bre = bre;
}
public void getArea () {
int area = len * bre;
System.out.println ("Area: " + area);
}
}
class BeanExample
{
public static void main (String[] args) {
Area rec = new Area (2, 16);
rec.getArea ();
}
}
Output:

Structure Of Java Bean Class
public class JavaBean
{
private String student;
public void setName (String student)
{
this. student = student;
}
public void getName ()
{
return student;
}
}
Example of Java Bean Program:
MyGoal.java
package MyGoal
public class Student implements java.io.Serializable{
private int id;
private String name;
public Student (){}
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;
}
}
Student1.java
package MyGoal;
public class Student1
{
public static void main (String args[])
{
Student s1=new Student(); // Object Creation
s1. setName ("Abhinav"); // setting value of the object
System.out.println (s1. getName ());
}
}
Output:

Accessing the Java Bean Class
- We must use getter and setter methods, to access the Java Bean class.
The syntax for setter methods:
- The nature of the method must be in public.
- The return type of method must be void.
- The method name must start with set.
- It should consider some arguments which are passing through the method.
Syntax:
public void setMethod_Name(datatype1 var1, datatype2 var2, datatype3 var3, ….., datatypen varn)
{
body
}
Program for Setting Methods:
// Creating Department Package
Department.java
package Department;
public class Course implements java.io.Serializable
{
private int id;
private String name;
public Course ()
{
}
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;
}
}
// Accessing Department package
Course1.java
package Department;
public class Course1
{
public static void main (String args[])
{
Course c1 = new Course (); // object creation of class
s.setName ("Artificial Intelligence"); // Setting names to object
System.out.println (s.getName ());
}
}
Output:

The Syntax for Getter methods:
- The nature of the method must be in public.
- The return type of method must not be void.
- The method name must start with get.
- It should not consider some arguments which are passing through the method.
Syntax:
public return_datatype getMethodname ()
{
Body
return variable;
}
// Example of Getter methods Boolean type of data
public class JavaTpoint
{
private boolean emp;
public boolean getName ()
{
return emp;
}
public boolean isempty ()
{
return emp;
}
}
Program for Setting Methods:
Department.java
package Department;
public class Course implements java.io.Serializable
{
private int id;
private String name;
public Course ()
{
}
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;
}
}
// Accessing Department package
Course2.java
package Department;
public class Course2
{
public static void main (String args[])
{
Course c1 = new Course (); // object creation of class
s.setName ("Data Science"); // Setting names to object
System.out.println (s.getName ());
}
}
Output:

Serialization:
It converts the state of an object into a byte stream, Deserialization is the reverse process of Serialization.
We should import java.io.Serializable to make the object serializable