0% found this document useful (0 votes)
87 views2 pages

JavaBeans Basics for Developers

JavaBeans allow reusable software components to be modeled in Java so they can be assembled into sophisticated applications. The JavaBeans specification defines rules for creating these components, including naming patterns for properties. Properties are private fields with getter and setter methods following standard naming conventions - get/is prefixes for getters and set prefixes for setters, with the property name capitalized. For example, a Light component may have properties for wattage, location, and on/off indicator, with corresponding getNoOfWatts(), setNoOfWatts() methods.

Uploaded by

aditya_goel27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views2 pages

JavaBeans Basics for Developers

JavaBeans allow reusable software components to be modeled in Java so they can be assembled into sophisticated applications. The JavaBeans specification defines rules for creating these components, including naming patterns for properties. Properties are private fields with getter and setter methods following standard naming conventions - get/is prefixes for getters and set prefixes for setters, with the property name capitalized. For example, a Light component may have properties for wattage, location, and on/off indicator, with corresponding getNoOfWatts(), setNoOfWatts() methods.

Uploaded by

aditya_goel27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA BEANS INTRODUCTION

The JavaBeans Standard allows reusable software components to be modelled in Java so that
these components can be assembled to create sophisticated applications.In particular, builder
tools can take advantage of how these components are specified, in order to build new
applications based on these components. The Java-Beans specification specifies the rules for
defining such components (called Java-Beans). The interested reader is encouraged to consult
this
documentation
(see
http://java.sun.com/javase/technologies/desktop/javabeans/docs/spec.html) for details since we
only cover the basic fundamentals for creating JavaBeans .
Naming Patterns for Properties

08

The rules of the JavaBean specification stipulate naming patterns for declaring properties of
JavaBeans. A naming pattern defines a standard naming convention. A property of an object is
normally defined as a field in the object, which is usually not directly accessible by clients (see
Example 3.1). A JavaBean should adhere to the following naming patterns when specifying its
properties:

IT
-2

The properties are assumed to be private, and their names start with a lowercase letter. Example
shows that the JavaBean class Light has three properties.
In order to retrieve and change values of its properties, a JavaBean provides getter and setter
methods for them. Example shows a JavaBean with three getter and three setter methods for its
properties.
For a property, the setter method starts with the prefix set. The rest of the method name is
assumed to be a property name, where the first letter of the property name has been converted to
uppercase. In Example, the value of the property noOfWatts can be changed by the setter method
setNoOfWatts(). Setter methods are public and void, having a parameter of the same type as that
of the property.
For a property, the getter method starts with the prefix get. The rest of the method name is
assumed to be a property name, where the first letter of the property name has been converted to
uppercase. In Example, the value of the property noOfWatts can be retrieved by the getter
method getNoOfWatts(). For a boolean property, the getter method can start with the prefix get
or is. In Example 3.1, the value of the boolean property indicator can be retrieved by the getter
method isIndicator(). Getter methods are no-argument public methods that return a value of the
same type as the parameter of the corresponding setter method.

15/05/14

Page 1 of 2

Example A JavaBean
public class Light {
// Properties:
private int noOfWatts; // wattage
private String location; // placement

// Setters

08

private boolean indicator; // on or off

public void setNoOfWatts(int noOfWatts) { this.noOfWatts = noOfWatts; }


public void setLocation(String location) { this.location = location; }

IT
-2

public void setIndicator(boolean indicator) { this.indicator = indicator; }


// Getters

public int getNoOfWatts() { return noOfWatts; }


public String getLocation() { return location; }

public boolean isIndicator() { return indicator; }

15/05/14

Page 2 of 2

You might also like