×

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

Sphenic Number in Java

In this section, we will learn what is a sphenic number is and show you how to write Java programmes to determine if a specific number are sphenic or not....

3 minutes read.

Java Continue Keyword

The java keyword ‘continues’ has also been called as java continue statement.The main concepts of the java continue statement or keyword is used in the controlling of the loop structure.Java...

3 minutes read.

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.

Camel Case in Java

Java names its classes, interfaces, methods, as well as variables using camel-case syntax. If the name consists of two words, the second word will always begin with a capital letter,...

3 minutes read.

Difference Between replace() and replaceall() in Java

In this article, you will be acknowledged about the replace() and replaceall() methods in java. Also, most importantly you will be acknowledged the differences between them along with the example...

4 minutes read.

Java ArrayList

Java ArrayList Class A Java ArrayList class is a dynamic array which is used to store the elements. It is a part of collection framework. It implements the List Interface and inherits the...

12 minutes read.

Java Viva Questions and Answers

Question: Explain Java programming language. Answer: It is a high-level programming language. This programming language is based on the principles of object-oriented programming. Large-scale applications can be developed by using this...

16 minutes read.

How to Reverse a String in Java

How to Reverse a String in Java There are a lot of ways to reverse a string in Java. One can use iteration, StringBuilder, StringBuffer to do the reverse of a...

6 minutes read.

Java Enhanced For Loop (For-each Loop)

JAVA ENHANCED FOR LOOP The enhanced for loop is also called the for-each loop and has some advantages over the regular for loop. A for-each loop is designed to cycle through...

4 minutes read.

Java String format() method

format() method returns a formatted String based on the given locale,specified format and arguments. Syntax: public static String format(String format , Object… args) Parameter: locale : It specifies locale value to be applied on...

2 minutes read.

Java Integer highestOneBit()

The highestOneBit() method of Java Integer class returns an int value with at most a single one-bit, in the position of the highest-order one-bit in the specified int value.  Syntax public static...

1 minute read.

Alien language problem in Java

Given the alphabetic sequence of an alien language, given a sorted dictionary (array of words) for the languages. Example: Words = { "aac", "abc", "aaa" } Output c, a, b Algorithm: (1) Compare two words that...

3 minutes read.

Java Switch Keyword

In this article we are going to learn the concept of a java switch keyword. Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in...

3 minutes read.

Java OCR

In this article, you will be acknowledged about what is a tesseract OCR, how it works, what are its used and advantages and disadvantages. Also, you will be able to...

4 minutes read.

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

Java Byte Code

Java byte code is really a powerful mechanism which makes Java a portable and platform-independent programming language. There are two software components which go along and make this byte code...

3 minutes read.

How to Convert int to double in Java

How to Convert int to double in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

2 minutes read.

Chromatic Number in Java

The chromatic number is the bare minimum of colours necessary to accurately colour any graph. To put it another way, the chromatic number can be thought of as the bare...

6 minutes read.

Collection Interfaces in Java with Examples

In this tutorial, we will discuss collection interfaces in Java with Examples. Introduction The Collection Interface is an individual from the Java Collections Framework. It is a root point of interaction of...

4 minutes read.

Composition in Java

Composition Java uses the composition method to implement a has-a connection. Composition allows us to reuse code in the same way that Java inheritance does. The "is-a" relationship is implemented using the...

3 minutes read.