Use constants in Java beans?

  • Thread starter Kai Grossjohann
  • Start date
K

Kai Grossjohann

I skimmed the JavaBeans specification, but didn't read all of it. So
maybe I missed the important part. Feel free to just direct me to the
right spot...

I'm implementing a bean that has a list of items, at most one of which
is selected at any time. All instances of this class have two
standard items, but it is possible to add more items at runtime.

For my purpose, strings seem to be sufficient as items. So I have a
read/write property currentItem of type string which contains the
current item. I also have a read-only property none, where setting
currentItem to none means that none of the items is selected. Then I
have read-only properties one and two, where setting currentItem to
one or two means that this standard item is selected.

Of course, the read-only properties amount to methods getOne(),
getTwo(), and getNone() in the class. This seems weird. It's not
possible to see that these methods are really used as constants.

Is there a way to explicitly express in the JavaBeans convention that
there are constants ONE, TWO, and NONE, of type string that can be
used as the value of the property currentItem?

Kai
 
J

John C. Bollinger

Kai said:
I skimmed the JavaBeans specification, but didn't read all of it. So
maybe I missed the important part. Feel free to just direct me to the
right spot...

I'm implementing a bean that has a list of items, at most one of which
is selected at any time. All instances of this class have two
standard items, but it is possible to add more items at runtime.

For my purpose, strings seem to be sufficient as items. So I have a
read/write property currentItem of type string which contains the
current item. I also have a read-only property none, where setting
currentItem to none means that none of the items is selected. Then I
have read-only properties one and two, where setting currentItem to
one or two means that this standard item is selected.

Of course, the read-only properties amount to methods getOne(),
getTwo(), and getNone() in the class. This seems weird. It's not
possible to see that these methods are really used as constants.

Is there a way to explicitly express in the JavaBeans convention that
there are constants ONE, TWO, and NONE, of type string that can be
used as the value of the property currentItem?

You can define static constants of any type on a bean. You can also
define instance methods ala selectOne(), selectTwo(), and selectNone().
The latter is better for what you want to do than the solution you
actually asked about, IMO.

An alternative would be to provide a BeanInfo for your bean containing a
PropertyDescriptor for the properties in question, which explained their
intended use. That places no programmatic constraints whatsoever, and
would usually only be visible to application assembly tools.


In general, you can do anything with a JavaBean clas that you can with
any other Java class, although some things do fit better with the
JavaBeans philosophy than others.


John Bollinger
(e-mail address removed)
 
K

Kai Grossjohann

John C. Bollinger said:
You can define static constants of any type on a bean. You can also
define instance methods ala selectOne(), selectTwo(), and selectNone().
The latter is better for what you want to do than the solution you
actually asked about, IMO.

Ayee. Some day, I will learn to say in my question everything that
needs to be said. Until then, we'll have to muddle through. I
apologize.

Actually, I would like to use the bean from JSP, via <jsp:useBean
..../>. My understanding of the JSP spec says that I can invoke
<jsp:setProperty .../> and <jsp:getProperty .../> on a bean, and this
seems to say that I can only invoke setter and getter methods.

Hm. Of course, I could say <jsp:setProperty ... value="<%=
TheBean.SOME_CONSTANT %>"/>. But I was trying to avoid this. Hm.

Kai
 
J

John C. Bollinger

Kai said:
Ayee. Some day, I will learn to say in my question everything that
needs to be said. Until then, we'll have to muddle through. I
apologize.

Actually, I would like to use the bean from JSP, via <jsp:useBean
.../>. My understanding of the JSP spec says that I can invoke
<jsp:setProperty .../> and <jsp:getProperty .../> on a bean, and this
seems to say that I can only invoke setter and getter methods.

Hm. Of course, I could say <jsp:setProperty ... value="<%=
TheBean.SOME_CONSTANT %>"/>. But I was trying to avoid this. Hm.

JSP does not provide built-in actions for invoking bean methods other
than setters and getters, but you can always invoke them via scriptlet
or custom tag. One of the effects of a useBean action is to define a
scripting variable, local to the page and named according to the
specified bean id, that contains a reference to the bean instance. You
can do whatever you want with that. For example:

<jsp:useBean id="arabica" class="org.beans.ArabicaBean" scope="request" %>
<jsp:setProperty name="arabica" property="amount" value="8">
<%! Coffee coffee; %>
<%
CoffeeMaker mrCoffee = new CoffeeMaker();
arabica.grind();
coffee = mrCoffee.brew(arabica);
%>
<!-- template text: -->
Mmmm... brewed <%=arabica.getAmount()%> cups of coffee.


JavaBeans would be much less useful in JSP if you were restricted to
just the built-in actions. As it is, in JSP, just like anywhere else,
they are little different from any other object.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top