What is a Java Bean?

 

092317_2206_WhatisJavaB1 What is a Java Bean?

 

What if you want to create your own Java Object with a set of properties. Let’s take an example, A User Sign-Up form. The Sign-up form contains many properties, username, password, sex, Date of Birth, Country, Alternate email address, etc. We know these set of properties belonging to one person. The best way to store these properties is to insert these properties as a record in a database table. What If I want a similar feature in Java, means I want to store these properties in a Java Object. Yes, In Java Its called as Java Bean. 

A Java bean is an encapsulation of many properties in a single object. The property may be a primitive data type or another Java Object. Also, the Java bean must have a no-argument public constructor and can be serializable. Now, look at the following Java Bean 

 

 

Click on the link for how to create these setter and getter methods automatically in eclipse. 

 

As you notice in the above Java class. It has a set of Java variables, setter methods, and getter methods. As a general rule, A java bean must follow the following patterns

  • The method names must start with get or set or is
  • The set method must have a parameter and no return type
  • The get method must not have any parameter and must return a type
  • The Boolean method can start with is or get

 

You may question why these setter getter methods are used for? Here below is the answer

 

  • The setter methods are for WRITING data into a java object.
  • The Getter methods are for READING data from a java object

 

Implementing serializable interface in a Java Bean class is not mandatory, but it’s a good practice. In case your java bean object transfers over a network then you must implement the serializable. For example, if you deploy your Java Bean in a clustered server then the object needs to travel through servers in that cluster so serializable is must in this case.

 

The following example illustrates how to Read and Write a Java bean. This class inserts a USER object using SETTER methods and prints USER object using GETTER methods 

 

 

Output:

Username = catgovind

Password = password123

Books = [Java Programming]

Country = India

Is Senior Citizen ? false

 

 

We can refer the bean properties directly in a JSP or a JSF page using Expression Language. The syntax is like below

 

In JSP, we can refer a bean property by ${user.username}

In JSF, we can refer a bean property by #{user.username}

 

This is called as Value expression, that I will cover in my next post.

govindan What is a Java Bean?
Connect me

Govind

Thank you for visiting my personal blog. Myself Govindan, Software Developer by profession since 2006 and hence I started this blog early in 2016 and ever since I've been writing about technologies experienced and learnings of everyday life.

The views expressed on this blog are my personal views and do not necessarily reflect the views of my employer.

Please feeling free to reach me on any comments and feedbacks you have. Would be more than glad to listen and reply 🙂
govindan What is a Java Bean?
Connect me
By | 2017-09-23T21:07:43+00:00 September 23rd, 2017|Categories: Java, JSF|Tags: , , |0 Comments
Like us on Facebook.
Connect!