UNIVERSITY INSTITUTE OF ENGINEERING
DEPARTMENT OF AIT - CSE
Bachelor of Engineering (CSE)
Programming in Java (21CSH-244)
By: Kushagra Agrawal (E13465)
Lecture -25 DISCOVER . LEARN . EMPOWER
Java Beans
Chapter Course Objectives
● To understand about Java Beans.
17. ● To understand getter and setter methods.
Chapter Course Outcomes
After completion of this course, student will be able to
● Learn the basic about Java Beans.
17. ● Learn about getter and setter methods
2
JavaBean
JAVA Beans
A JavaBean is a Java class that should follow the following conventions:
• It should have a no-arg constructor.
• It should be Serializable.
• It should provide methods to set and get the values of the properties, known as getter and setter
methods.
Why use JavaBean?
• According to Java white paper, it is a reusable software component.
• A bean encapsulates many objects into one object so that we can access
this object from multiple places. Moreover, it provides easy
maintenance.
public class Employee implements 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;}
}
How to access the JavaBean class?
• To access the JavaBean class, we should use getter and setter methods.
package mypack;
public class Test{
public static void main(String args[]){
Employee e=new Employee();//object is created
e.setName("Arjun");//setting value to the object
System.out.println(e.getName());
}}
6
JavaBean Properties
• A JavaBean property is a named feature that can be accessed by the user of the object.
The feature can be of any Java data type, containing the classes that you define.
• A JavaBean property may be read, write, read-only, or write-only. JavaBean features are
accessed through two methods in the JavaBean's implementation class:
1. getPropertyName ()
For example, if the property name is firstName, the method name would be
getFirstName() to read that property. This method is called the accessor.
2. setPropertyName ()
For example, if the property name is firstName, the method name would be
setFirstName() to write that property. This method is called the mutator.
7
Advantages of JavaBean
The following are the advantages of JavaBean:/p>
• The JavaBean properties and methods can be exposed to another application.
• It provides an easiness to reuse the software components.
Disadvantages of JavaBean
The following are the disadvantages of JavaBean:
• JavaBeans are mutable. So, it can't take advantages of immutable objects.
• Creating the setter and getter method for each property separately may lead to the
boilerplate code.
8
Summary
. Discussed about Java Beans.
. Discussed about getter and setter methods.
9
Home Work
Q1. Difference between Java Bean and Bean?
Q2. What is a Bean? Why is not a Bean an Applet?
10
References
Online Video Link
• https://nptel.ac.in/courses/106/105/106105191/
• https://www.coursera.org/courses?query=java
• https://www.coursera.org/specializations/object-oriented-programming
• https://www.youtube.com/watch?v=aqHhpahguVY
Text Book
• Herbert Schildt (2019), “Java The Complete Reference, Ed. 11, McGraw-Hill .
11
THANK YOU
For queries
Email: [email protected]