×

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

Types of Sockets in Java

The fundamental idea behind Java's networking capability is the socket. Early in the 1980s, the Berkeley UNIX 4.2BSD version included the socket paradigm. Berkeley socket is the term employed as...

9 minutes read.

System Class in Java

The System class provides features including a way to load files and libraries, standard input, standard output, and errors throughput streams, accessibility to outside defined characteristics and environment variables, and...

3 minutes read.

Java Boolean hashCode() Method

The hashCode() method of Boolean class returns the hash code for the specified Boolean object or the given Boolean value. Syntax public int hashCode()public int hashCode(boolean value) Parameters The parameter ‘value’ represents the value...

2 minutes read.

Java Comparable Interface

We implement comparable interface when we need a new logic for determining equality. if two objects are equal, the equals() method returns true and compareTo() method returns 0. The basic...

1 minute read.

Compile time vs Runtime in java

Introduction: This article will discuss compile time vs. runtime in java. Compile time and runtime are two programming terms utilized in software improvement. Compile time is when the source code is...

3 minutes read.

Java Logo

Java is a prominent and extensively used object-oriented programming language. In 1995, Sun Microsystems created it. Later in 2009, Oracle Corp takeover Java. History of Java Logo The name of the island...

3 minutes read.

Equidigital in Java

In this section, we will understand what is an equidigital number and how to write Java programs to locate them. It is commonly asked in academic settings and Java coding...

4 minutes read.

Java Math floor() Method

The floor() method of Math class returns the largest double value that is equal to a mathematical integer and is less than or equal to the argument. Syntax: public static double floor(double...

2 minutes read.

Program to find the duplicate characters in a string

Problem statement You have given with a string and your task is to find out the repeated characters from the string and print them. If no character is repeated, then you...

2 minutes read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.

Java Interface Keyword

An interface is also known as the blueprint in Java. It has constants of static values and methods of abstraction. The interface is a mechanism used by Java to declare...

3 minutes read.

Zigzag Array in Java

In this tutorial, we discuss, what is zigzag array and its example. Even we will create the java program. In this program, we convert the simple array into a zigzag...

4 minutes read.

Java Math log10() Method

The log10() method of Math class returns the base 10 logarithmic value for the specified double argument. Syntax: public static double log10(double a) Parameters: The parameter ‘a’ represents a double value. Return Value: The log10() method...

2 minutes read.

Java DatagramSocket and Java DatagramPacket

Datagrams TCP/IP style networking specifies a serialized, predictable, and reliable stream of data in the form of a packet. Servers and clients communicate through a reliable channel, such as TCP socket, have a dedicated...

6 minutes read.

GCD Program in Java

GCD Program in Java The GCD program in Java outputs the GCD of the given numbers. In mathematics, Greatest Common Divisor (GCD), Greatest Common Factor or Highest Common Factor (HCF) of...

14 minutes read.

Why main method is static in Java

The method serves as the entry point for Java programmes or simply the point from which the programme begins to run. As a result, it is one of the most...

4 minutes read.

Switch Case with Enum in Java

From some conditions, the java switch statement executes one statement. Similar to the If-Else-If ladder statement, this can be Byte, short, int, long, enum, string, and some wrapper types like...

4 minutes read.

Java Integer longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute read.

Java Stringjoiner Class

StringJoiner is a class which is used to construct a sequence of characters which are separated by a delimiter. Optionally, it starts with a provided prefix and ended with the...

5 minutes read.

Creation of Multi Thread in java

What are threads in Java? We can use threads to facilitate parallel processing. Threads are helpful when you wish to execute several pieces of code concurrently. A thread is a small process...

3 minutes read.