Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is JavaBeans? Introduction to JavaBeans Concepts

Last updated on Jun 17,2021 30.8K Views

7 / 22 Blog from Advance Java

Reusability is the main concept in any programming language. A JavaBean is a software component that has been designed to be reusable in a variety of environments. Let us dive into the topic and understand the horizons of concept in this ‘What is JavaBeans’ article.

Following pointers will be our topics of discussion:

Let’s begin!

What is JavaBeans?

JavaBeans is a portable, platform-independent model written in Java Programming Language. Its components are referred to as beans. 

In simple terms, JavaBeans are classes which encapsulate several objects into a single object. It helps in accessing these object from multiple places. JavaBeans contains several elements like Constructors, Getter/Setter Methods and much more.

JavaBeans has several conventions that should be followed:

  • Beans should have a default constructor (no arguments)
  • Beans should provide getter and setter methods
    • A getter method is used to read the value of a readable property
    • To update the value, a setter method should be called
  • Beans should implement java.io.serializable, as it allows to save, store and restore the state of a JavaBean you are working on

Now that you are familiar with basics, let’s learn in detail about the properties of JavaBeans.

What are JavaBean Properties?

A JavaBean property can be accessed by the user of the object. The feature can be of any Java data type, containing the classes that you define. It may be of the following mode: read, write, read-only, or write-only. JavaBean features are accessed through two methods:

 

1. getEmployeeName ()

For example, if the employee name is firstName, the method name would be getFirstName() to read that employee name. This method is known as an accessor. Properties of getter methods are as follows:

  1. Must be public in nature
  2. Return-type should not be void
  3. The getter method should be prefixed with the word get
  4. It should not take any argument

2.setEmployeeName ()

For example, if the employee name is firstName, the method name would be setFirstName() to write that employee name. This method is known as a mutator. Properties of setter methods:

  1. Must be public in nature
  2. Return-type should be void
  3. The setter method has to be prefixed with the word set
  4. It should take some argument

Now that you have attained some theoretical knowledge about JavaBeans, let us move on and understand the implementation process.

Example Program: Implementation of JavaBeans

The example program shown below demonstrates how to implement JavaBeans.

public class Employee implements java.io.Serializable 
{ 
private int id; 
private String name; 
public Employee() 
    { 
    } 
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; 
    } 
} 

Next program is written in order to access the JavaBean class that we created above:

public class Employee1 {
public static void main(String args[])
{
	Employee s = new Employee(); 
    s.setName("Chandler"); 
    System.out.println(s.getName()); 
}
}

Output:

Chandler

So, that’s how to implement a Java program which accesses the JavaBean class.

Advantages of JavaBeans

The following list enumerates some of the benefits of JavaBeans:

Portable

JavaBeans components are built purely in Java, hence are fully portable to any platform that supports the Java Run-Time Environment. All platform specifics, as well as support for JavaBeans, are implemented by the Java Virtual Machine.

Compact and Easy

JavaBeans components are simple to create and easy to use. This is an important focus sector of the JavaBeans architecture. It doesn’t take much effort to write a simple Bean. Also, a bean is lightweight, so, it doesn’t have to carry around a lot of inherited baggage to support the Beans environment.

Carries the Strengths of the Java Platform

JavaBeans is pretty compatible, there isn’t any new complicated mechanism for registering components with the run-time system.

Though all these sound good, using JavaBeans presents some disadvantages as well. Now, let’s check out what those would be.

Disadvantages of JavaBeans

  1. JavaBeans are mutable, hence lack the advantages offered by immutable objects.
  2. JavaBeans will be in inconsistent state partway through its construction.

With this, we have reached the end of this “What is JavaBeans” article. I hope that the contents explained discussed here added value to your Java knowledge. Well, keep exploring the Java world. Stay tuned!

Check out the Java Certification Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “What is JavaBeans’’ blog and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is JavaBeans? Introduction to JavaBeans Concepts

edureka.co