JavaBeans: Difference between revisions

Content deleted Content added
Shvahabi (talk | contribs)
NotAG on AWB (talk | contribs)
replace {{manual}} with {{how-to}} per TfD
 
(12 intermediate revisions by 11 users not shown)
Line 1:
{{Short description|Computing technology developer by Sun Microsystems}}
{{Distinguish|Enterprise JavaBeans}}
{{multiple issues|
{{refimprovemore citations needed|date=June 2016}}
{{manualhow-to|date=October 2012}}
}}
 
In computing based on the [[Java (programming language)|Java]] Platform, '''JavaBeans''' is a technology developed by [[Sun Microsystems]] and released in 1996, as part of [[Java Development Kit|JDK]] 1.1.
 
The 'beans' of JavaBeans are classes that encapsulate one or more [[Object (computer science)|objects]] into a single standardized object (the bean). This standardization allows the beans to be handled in a more generic fashion, allowing easier [[code reuse]] and [[Type introspection|introspection]]. This in turn allows the beans to be treated as [[Component-based software engineering|software components]], and to be manipulated visually by [[Integrated development environment|editors and IDEs]] without needing any initial configuration, or to know any internal implementation details.
 
As part of the standardization, all beans must be [[Serialization|serializable]], have a [[nullary constructor|zero-argument constructor]], and allow access to properties using [[Mutator method|getter and setter methods]].
Line 23 ⟶ 24:
:Persistence is the ability to save the current state of a Bean, including the values of a Bean's properties and instance variables, to nonvolatile storage and to retrieve them at a later time.
;Methods
:A beanBean should use [[Mutator_methodMutator method#Java_exampleJava example|accessor methods]] to [[Encapsulation_Encapsulation (computer_programmingcomputer programming)|encapsulate]] the properties. A beanBean can provide other methods for business logic not related to the access to the properties.
 
== Advantages ==
 
* The properties, events, and methods of a bean can be exposed to another application.
* A bean may register to receive events from other objects and can generate events that are sent to those other objects. {{citation needed|date=January 2023}}
* Auxiliary software can be provided to help configure a bean. {{citation needed|date=January 2023}}
*The configuration settings of a bean can be saved to persistent storage and restored. {{citation needed|date=January 2023}}
 
== Disadvantages ==
* A class with a [[nullary constructor|zero-argument constructor]] is subject to being instantiated in an invalid state.<ref name="Bloch">{{cite book|last1=Bloch|first1=Joshua|authorlink1=Joshua Bloch|title=Effective Java|date=2008|publisher=Addison-Wesley|isbn=978-0-321-35668-0|page=[https://archive.org/details/effectivejava00bloc_0/page/13 13]|edition=Second|url-access=registration|url=https://archive.org/details/effectivejava00bloc_0/page/13}}</ref> If such a class is instantiated manually by a developer (rather than automatically by some kind of framework), the developer might not realize that the class has been improperly instantiated. The compiler cannot detect such a problem, and even if it is documented, there is no guarantee that the developer will see the documentation.
* JavaBeans are inherently mutable and so lack the advantages offered by [[immutable objects]].<ref name="Bloch"/>
* Having to create getters for every property and setters for many, most, or all of them can lead to an immense quantity of [[boilerplate code]]. This can be mitigated using tools like [https://www.baeldung.com/intro-to-project-lombok Lombok].
 
== JavaBeans API ==
 
The JavaBeans functionality is provided by a set of classes and interfaces in the <ttcode>java.beans</ttcode> package.
{| class="wikitable sortable"
|-
Line 116 ⟶ 117:
/**
* Getter for property "deceased"
* Different syntax for a boolean field (is v.s.vs get)
*/
public boolean isDeceased() {
Line 180 ⟶ 181:
</html>
</syntaxhighlight>
 
== See also ==
* [[Software package (disambiguation)|Software packaging]]
 
==References==
Line 191 ⟶ 195:
[[Category:Java platform]]
[[Category:Articles with example Java code]]
[[Category:Architectural pattern (computer science)]]
[[Category:Software design patterns]]