Setters that return the Object

Does the JavaBean specification allow for a bean setter to return the object that it sets a property for? Suppose you had a bean like so:

public class Fish ( ) {

private String color ;

private int length ;

public String getColor ( ) { return color ; }

public Fish setColor ( String color ) {

this.color = color ;

return this ;

}

public int getLength ( ) { return length ; }

public Fish setLength ( int length ) {

this.length = length ;

return this ;

}

}

Then, I could call all of the setters at once:

Fish bailey = new Fish() ;

bailey.setColor("gold").setLength(2) ;

Is this kosher?

Anthony

[757 byte] By [ inertia] at [2007-9-26 17:25:41]
# 1

While there is nothing preventing you from implementing your setters that way, I would recommend not doing so.

You're not really improving on anything except typing your code a few seconds faster. In fact readability suffers -- bugs would be more elusive. And from a pure design standpoint a setter should almost always have a void return.

noitall at 2007-7-2 22:27:57 > top of Java-index,Desktop,Developing for the Desktop...