I\'ve bean doing some JSP tutorials and I don\'t understand what the point of a bean class is. All it is, is get and set methods. why do we use them?
public
A Bean obtains all the benefits of Java's "write-once, run-anywhere" paradigm. The properties, events, and methods of a Bean that are exposed to an application builder tool can be controlled.
A Bean may be designed to operate correctly in different locales, which makes it useful in global markets.
Auxiliary software can be provided to help a person configure a Bean. This software is only needed when the design-time parameters for that component are being set. It does not need to be included in the run-time environment. The configuration settings of a Bean can be saved in persistent storage and restored at a later time.
A Bean may register to receive events from other objects and can generate events that are sent to other objects.
The use of scriptlets (those <% %>
things) is indeed highly discouraged since the birth of taglibs (like JSTL) and EL (Expression Language, those ${}
things) over a decade ago. The major disadvantages of scriptlets are:
Reusability: you can't reuse scriptlets.
Replaceability: you can't make scriptlets abstract.
OO-ability: you can't make use of inheritance/composition.
Debuggability: if scriptlet throws an exception halfway, all you get is a blank page.
Testability: scriptlets are not unit-testable.
Maintainability: per saldo more time is needed to maintain mingled/cluttered/duplicated code logic.
You can have JSP without beans, however, the result will be that both the code taking care of the logic and the code taking care of the aesthetics of the site will be jumbled up. This is not ideal especially since in most situations, a group of people codes the logic and another group of people take care of the aesthetics, so beans allow a degree of separation which makes life easier for the people building the website.
Beans also allow you to reuse the code, which is another reason why you should use them.
The main reason for JavaBeans is for reusability. JavaBeans can be used in JSP's, Servlets, and other java technologies.
It's a simple serializable objects that is used to encapsulate many objects into one. I.e, we can send a bean fully defined as to sending each attribute individually across the wire.
The accessor(getter) and mutator(setter) methods are generally used to provide encapsulation. As the instance variables are private they can only be accessed outside the class through these methods.
So In layman : the ability to make change in your implementation code without breaking the code of others who use your code is key benefit of encapsulation.
I was reading JavaFX Properties and Binding there I got practically meaning of JavaBeans here is the paragraph.
The Java programming language makes it possible to encapsulate data within an object, but it does not enforce any specific naming conventions for the methods that you define. For example, your code might define a Person class, which encapsulates a first name and a last name. But without naming conventions, different programmers might choose different names for these methods: read_first(), firstName(), getFN(), etc. would all be perfectly valid choices. However, there is no guarantee that these names will be meaningful to other developers.
The JavaBeans component architecture addressed this problem by defining some simple naming conventions that bring consistency across projects. In JavaBeans programming, the full signatures for these methods would be: public void setFirstName(String name), public String getFirstName(), public void setLastName(String name), and public String getLastName(). This naming pattern is easily recognizable, both to human programmers and to editing tools, such as the NetBeans IDE. In JavaBeans terminology, the Person object is said to contain firstName and lastName properties.
The JavaBeans model also provides support for complex property types, plus an event delivery system. It also contains a number of support classes, all available as an API under the java.beans package. Therefore, mastering JavaBeans programming involves learning the required naming conventions and its corresponding API. (For more background reading on JavaBeans in general, see the JavaBeans lesson of the Java Tutorial).
It mainly used for REUSABILTY. It's a simple serializable objects that is used to encapsulate many objects into one.