For Programmers: Free Programming Magazines  


Home > Archive > Java Beans > January 2005 > How to provide a reasonable default without a no-parameter-constructor?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to provide a reasonable default without a no-parameter-constructor?
Christian Schlichtherle

2005-01-24, 3:57 am

Hi everyone,

I am pretty new to JavaBeans, so please excuse the question: How do I
provide a blue print for the state of a JavaBean when a nullary constructor
just isn't adequate? Can this be done at all or is the provisioning of a
nullary constructor a MUST for a JavaBean to be a JavaBean?

(If this text sounds then that's only because its a reflection of
my mind :-)

Regards,
Christian


John C. Bollinger

2005-01-24, 4:00 pm

Christian Schlichtherle wrote:
> Hi everyone,
>
> I am pretty new to JavaBeans, so please excuse the question: How do I
> provide a blue print for the state of a JavaBean when a nullary constructor
> just isn't adequate? Can this be done at all or is the provisioning of a
> nullary constructor a MUST for a JavaBean to be a JavaBean?
>
> (If this text sounds then that's only because its a reflection of
> my mind :-)


In some sense, a nullary constructor _must_ be sufficient. That is to
say, in the most general sense you must assume that bean instances will
be initialized by invocation of a nullary constructor (and no other).
Bean classes are required to have one, and it is poor practice and will
cause you problems if that constructor does not leave the new instance
in a consistent, well-defined state.

With that out of the way, there are two non-exclusive possibilities:

1) Refactor your bean into two or more beans. It is often the case when
a class has complex initialization requirements that the class itself is
too broad in scope.

2) Define class-wide defaults by use of static methods and variables.
This is bit of a kludge, and it has potential to get you into trouble,
but it can be made to work. The nullary constructor then uses the class
methods and variables to help initialize new instances.


John Bollinger
jobollin@indiana.edu
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com