Java Beans and the relation to GUIs

50 Views Asked by At

I understand that a Java Bean is a Java class that adheres to the 3 conditions:

  1. Having a no-arg ctor
  2. Having private fields with setters and getters
  3. Be serializable

However, I'm having hard time to understand when is it useful and why. It seems from the spec that it has something to do with visual tools, as the spec defines What is a Java Bean:

“A Java Bean is a reusable software component that can be manipulated visually in a builder tool.”

The rest of the section goes on about visual builders and stuff. So do Java Beans only come handy in the context of visual builders?

1

There are 1 best solutions below

4
rzwitserloot On

The idea is that some UI tool can, based on nothing but the name of a class, show you a dialog box with its properties. It does that by inspecting its methods using reflection. It can figure out, for each property:

  • Its name (setFoo and getFoo both exist? foo is the name of this property).
  • Its type (void setFoo(String) exists, String getFoo() exists, therefore, foo is a property whose value is String).

This notion is about 30 years obsolete. Nobody and Nothing uses this concept or thinks about it. The idea never took off. One reason might be that this system is woefully inept:

  • You can't tell what kind of value is supposed to go there. The feature even predates enums, so the HTML equivalent of a <select> was not possible with this. There was no system to indicate what kind of values were allowed. For example, a property of type int could not indicate that its value must be positive. At best, the UI can offer a component that can allow any value, and will deal with IllegalArgumentException to tell you about it. But it can't give you a UI widget that is more fundamentally designed to do positive numbers only.
  • You can't control the UI in any way. You can't indicate, for example, that you'd prefer a scrollable line widget instead of a flippable number widget.
  • You can't describe the property, or introduce any sort of grouping or spacing as methods dont have an ordering inside a class (you can't put the 'last name' and 'first name' fields together). At best you can introduce setName(Name name) and then define name as class Name {String first, last; /* setters and getters here */} and hope that the UI does a reasonable job rendering this hierarchical take. But if you don't want the hierarchy, you just want 'first' and 'last' name together, there's no way to indicate this.

In general the idea of "I want to define a UI" was never even feasible with this system, and insofar that you really want that, HTML won that fight.

The second idea is that you have a 'live' system where you can 'live' edit things, which involves not just this beanspec thing but also that you can 'observe' changes, but this never took off either: Having a system that dynamically reacts to a value being changed by an outside force (such as that property being exposed in a UI so that a human can modify it) is complicated, and the complicated part is not in any way the actual exposing of the endpoint. It's writing a system to deal with it. Hence, the 'value' that the beanspec provides is zero, and if you've gone through the considerable hardship of writing a system that can react to dynamically changing properties, you do not toss out 50% of the value by failing to spend 1% of the effort on a slightly nicer UI for it than 'a name, a java type representing the data, but no way to modify in any way what it looks like, or add descriptions, or even group things together meaningfully, or affect how it looks'.

TL;DR: This isn't relevant. Whatever documentation or tutorial you are reading became completely irrelevant to the IT sphere about 25 years ago. Delete it.