Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

Why use JavaBean?

Divine Odazie

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

widget

In this shot, we will learn about JavaBean and why we use them when building Java applications.

To understand why we use JavaBean, we first need to know what a JavaBean is.

A JavaBean is a Java class that follows a certain standard:

  • JavaBean class properties should be set to private – accessing /mutating these properties is done using getters/setters methods.

  • A JavaBean class should have a public no-argument constructor.

  • A JavaBean class should implement the java.io.Serializable interface. The Serializable interface allows the saving, storing, and restoring of a JavaBean’s state while in use.

There is no syntactic difference between a regular Java class. The standard stated above is what differentiates an ordinary Java class from a JavaBean class.

Example JavaBean class

Let’s see an example of a simple JavaBean class.

package demo;
public class User implements java.io.Serializable{ // implementing the Serializable interface
private int id; // private property
private String name; // private property
public User(){
// no-argument constructor
}
public void setId(int id){ // mutator method
this.id=id;
}
public int getId(){ // accessor method
return id;
}
public void setName(String name){ // mutator method
this.name=name;
}
public String getName(){ // accessor method
return name;
}
}
JavaBean.java

In the above code, we can see that:

  • The User class implements the java.io.Serializable interface.

  • All properties of the class are set to private.

  • Each property has a getter and setter method.

  • And the constructor has no argument at all.

Why Java Bean?

  • A fundamental principle of software development is “Don’t Repeat Yourself” (DRY); a JavaBean is a reusable software component that can be used all around a Java application, adhering to the DRY principle.

  • Java is an Object-Oriented Programming (OOP) Language. A core concept of OOP is encapsulation. A JavaBean encapsulates many objects into a single object (the bean).

  • We can expose JavaBean events, properties, and methods to another application.

  • Even though JavaBeans are designed to run on the client-side, one can develop server-side beans.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring