×

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:

Java Beans

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:

Java Beans

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:

Java Beans

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:

Java Beans

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


Related Topics

Java String join() method

Java String join() method returns a joined String with given delimiter Syntax: public static String join(CharSequence delimeter,charSequence...elements) public static String join(CharSequence delimeter,Iterable<?extens charSequence>elements) Parameters: delimiter: char value to be added with each element elements: char value...

1 minute read.

Java throw

Java throw Sometimes it is required in the code to throw an exception deliberately. In order to achieve the same, the Java throw keyword should be used. One can throw either...

3 minutes read.

Java Open File

Java Desktop class give an open() strategy to open a filr. It has a place with a java.awt package. Work area execution is stage subordinate, so it is important to...

5 minutes read.

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

Graphics Program in Java

Graphics Program in Java The graphics program in Java is part of the Swing program in Java. In this section, we will learn about the implementation of custom graphics using some...

6 minutes read.

Java Math negateExact() Method

The negateExact() method of Math class returns the negation for the specified argument, throwing an exception if the result overflows an int or a long. Syntax: public static int negateExact (int a)public...

1 minute read.

Java Identifiers

In Java, the symbolic notations used for identification are called identifiers. Identifiers can be the name for the class, variable name declaration, name of the package, constant name and many...

3 minutes read.

Generic Linked List in Java

A linear data structure known as a Linked List stores values in nodes. As we already know, each node has two properties: its value and a link to the node...

6 minutes read.

How to take String Input in Java

There are various ways to take String input in Java. In this section, we are going to discuss how to take String input in Java. There are following ways to...

5 minutes read.

How to create an array in Java

An array is a linear data structure stores a collection of fixed number ofelements of similar type. The size of an array is specified when it is created. The array...

14 minutes read.

RMI program in Java

Remote Method Invocation is what it stands for. An object can call the method of another object in a different space address using the RMI API, which may be on the...

3 minutes read.

Java Throw and Throws Keyword

Exceptions in Java enable us to construct high-quality programs where faults are checked at compile time rather than run time and where we may define unique exceptions that make code...

6 minutes read.

Java Obfuscator

Obfuscation is the process of making something unclear or difficult to interpret. Obfuscators are being used in programming to protect the source code from hackers. In this article, we will...

8 minutes read.

Cyclic Barrier in Java

Programmers often find it challenging to run multiple threads simultaneously. Java introduces the idea of concurrency, which enables us to run many threads concurrently, making this work simpler. Concurrent programming...

6 minutes read.

Functional Interfaces in Java

Java has forever remained an Object-Oriented Programming language. By object-oriented programming language, we can declare that everything present in the Java programming language rotates throughout the Objects, except for some...

11 minutes read.

Program to check whether a given character is present in a string or not

In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified...

3 minutes read.

Fibodiv Number in Java

Before understanding the concept of the fibodiv number, one should realize what the Fibonacci number is. What are Fibonacci Numbers? The Fibonacci sequence of whole numbers is composed of the numbers 0,...

5 minutes read.

Java exception list

Java uses exceptions, like the majority of contemporary programming languages, to deal with both errors and "extraordinary events." When an exception arises inside the program, it messes up the regular...

6 minutes read.

Special Operators in Java

An operator in Java is a special symbol. It is used to perform operations on two or more variables.  We all know basic operations in Java. There are 8 types...

5 minutes read.

Adapter class in Java

By using the adapter classes, we can implement Listener interfaces. With the help of adapter classes, we can save code as it provides all implementation methods of listener interfaces Advantages of...

3 minutes read.