Bean Internals  «Prev  Next»
Lesson 3JavaBean Methods
ObjectiveThis page discusses JavaBean Methods

JavaBeans Method Encapsulation

Like a normal Java class, a Bean is capable of having methods with different types of access. For example, private methods[1] are accessible only within the internals of a Bean, whereas protected methods are accessible both internally and in derived Beans. The most important methods for JavaBeans are public methods, since they form the primary means by which a JavaBean communicates with the outside world.
  • JavaBean Methods and Communication:
    Beans also communicate with the outside world through events, which are generated when the internal state of a Bean changes.
    You learn much more about events later in this course.
  • JavaBeans Component:
    A JavaBeans component is an object that conforms to a communication and configuration protocol, as prescribed by the JavaBeans specification. The JavaBeans specification prescribes programming conventions and dynamic discovery mechanisms that minimize the design and implementation effort for small software components while supporting the design, implementation, and assembly of complex components. The three fundamental aspects of the JavaBeans component as defined by the specification are events, properties, and methods.
  • Separation of Work Strategy:
    The success of the JavaBeans model is due in large part to its separation of work strategy. Consider the design of a common graphical component such as a progress bar. The fundamental task of displaying a percent-completion bar is quite easily handled with basic drawing operations. With the JavaBeans model, you can implement a basic progress bar Bean in 200 lines of code or less. That is, with respect to design and implementation, the model does not impose any significant up-front overhead. The majroity of the JavaBeans API is optional. Additional tasks such as supporting configurable foreground and background colors, vertical or horizontal configuration, and increment size are easily handled with minimal programming effort due to various API conventions.
    The JavaBeans architecture, a modern component architecture, is designed for building independent software components from which programmers assemble larger application components. With JavaBeans components, programmers have total freedom to assemble components using traditional programming strategies or to use graphical builder tools to connect JavaBeans components, or to combine both techniques.
    The JavaBeans architecture is the standard component architecture for Java technologies. The complete JavaBeans API is packaged in java.beans, one of the core Java APIs. This package includes interfaces and classes that support design or runtime operations. In developing a JavaBeans component, it is common to separate the implementation into design-only and runtime classes, so that the design-oriented classes (which assist programmers during component assembly) do not have to be shipped with a finished application.
  • Compact and Easy:
    JavaBeans components are simple to create and easy to use. This is an important goal of the JavaBeans architecture. It doe not take very much to write a simple Bean, and such a Bean is lightweight? It doesn't have to carry around a lot of inherited baggage just to support the Beans environment. If a Bean does not require the advanced features of the architecture, it doesn't get them, nor does it get the code that goes with them. This is an important concept. The JavaBeans architecture scales upward in complexity, not downward like other component models. This means it really is easy to create a simple Bean. The previous example shows just how simple a Bean can be.



Public methods in the Bean above communicating with interfaces A and B
Public methods in the Bean above communicating with interfaces A and B


JavaBean methods form Interfaces

Public Bean methods are often grouped according to their function, in which case they form interfaces. Beans expose their functionality to the outside world through interfaces. Interfaces[1] are important because they specify the protocol by which a particular Bean is interacted with externally. A programmer need only know a Bean's interfaces to be able to successfully manipulate and interact with the Bean.
  • Java Event Model:
    The JavaBeans architecture takes advantage of the event model that was introduced in version 1.1 of the Java language. Beans are treated the same as every other object within the event model. In fact, there is nothing at all about the event model that is specific to Beans.
    The Beans component model is designed to take advantage of features that are already available in Java. In many cases the features that are needed by Beans are generic enough to be provided by a core Java class library, making them available to any Java class whether or not it happens to be a Bean. An event source object sends a notification that an event occurred by invoking a method on the target, or "listening," object. The notification mechanism uses standard Java methods. There is no new language construct or programming syntax required to create such a method. Every event has a specific notification method associated with it. When the event occurs, the associated method is invoked on every object that is listening for it. The method itself describes to the caller which event has taken place. When this method is called, an object is passed as an argument that contains specific information about that particular instance of the event. This object always contains a reference to the object that was the source of the event. In addition, it allows the object receiving the event to identify the sender, and is particularly important if an object listens for the same event from several sources. Without this reference, we would have no way of determining which object sent the event and the combination of the method called and the data sent completely defines the event.
    The Java event model is comprised of event objects, event listeners, and event sources. These objects interoperate in a standard way, using method invocation to facilitate firing and handling events. The event listener registers itself with the event source to receive event notifications. At some point the event source fires an event with the event object as a parameter, and the event listener handles the event.

[1]private methods In Java, private methods are declared using the `private` access modifier. This restricts their accessibility to only within the same class where they are defined, aiding in encapsulation and information hiding.
[2] : Interface: Using interface, you can specify a set of methods that can be implemented by one or more classes. In its traditional form, the interface, itself, does not actually define any implementation.

SEMrush Software