Reputation: 15146
I was looking to JavaBean advtanges and disadvantages. In the disadvantages section, I came across this:
A class with a nullary constructor is subject to being instantiated in an invalid state. Wikipedia - JavaBeans
What exactly does this mean? How does this opppse to constructors that have parameter arguments?
Upvotes: 3
Views: 255
Reputation: 751
An object created by a nullary constructor will not set value to any of its properties. If a framework sets it, it will likely know which properties need to be set to what kind of values before the object can be used. However, a programmer, who is not familiar with the intention of the object, might not know or forget to set the properties to valid values. Even if there is documentation to stipulate what values need to be set for which parameters, a human programmer may not read that documentation or remember to follow all the rules.
For example, in the following code, if the object "data" is not set to a valid Enum type (such as null), an InvalidObjectException will be raised.
Object toJavaTypeData(Object data)
throws OpenDataException, InvalidObjectException {
try {
return Enum.valueOf(enumClass, (String) data);
} catch (IllegalArgumentException e) {
// missing enum constants
final InvalidObjectException ioe =
new InvalidObjectException("Enum constant named " +
(String) data + " is missing");
ioe.initCause(e);
throw ioe;
}
}
Upvotes: 0
Reputation: 48682
Consider a simple class that has a simple String
attribute called name. It might have a public String getName()
method to fetch the value of the attribute. Although the signature of that method says it returns a reference to a String
object, the design of your class might be more restrictive than that. The design might state that the name may not be null, may not be empty, and must begin with an upper case character, and must be unique, for example. Any object of that class that was ever observed to have a name that did not conform to all those restrictions would be invalid.
The job of the constructor is to set up a new object in its initial state, which must be a valid state. Some of the kinds of restrictions on values are impossible for a constructor to meet without the constructor being given addition information about how to construct the object.
A constructor without arguments can not be given that information, so there are some kinds of restriction the design of a Java Bean can not apply, or can apply only if the constructor is exempt from them. Being exempt from them really means allowing the constructor to create invalid objects.
Upvotes: 0
Reputation: 160211
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.
A JavaBean framework may perform automatic operations on an instantiated JavaBean, for example, by calling methods, passing parameters to regular methods, etc. depending on what the JavaBean is designed to do, and how the framework is expected to process JavaBeans under its control.
The compiler can’t detect such a problem, and even if it’s documented, there’s no guarantee that the developer will see the documentation.
Rephrased: "Developers aren't always diligent, and even when they are, they're not always right."
Components designed explicitly for consumption by automated tools aren't always well designed for consumption by actual people.
Upvotes: 2
Reputation: 35061
Consider java.net.URL
Let's suppose it has a null constructor, and a setter for the actual url string, port, etc. When set up using the null constructor, it will be in an invalid state, and anyone trying to use that URL object will have a problem.
URL clearly doesn't have a null constructor. I used this just to highlight the point that for some objects, having a null constructor can initialize them in an invalid state
Upvotes: 1