The Artima Developer Community
Sponsored Link

Extra Invocations, Extra Eye Hops



This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Extra Invocations, Extra Eye Hops

Posted by Bill Venners on 07 May 1998, 5:40 PM

> How about this

> public class CoffeeCup {

> public final static MIN_INNER_COFFEE = 0;

> // defs of sizes...
> private int innerCoffee;
> private int size;

> public CoffeeCup() {
> this(SHORT);
> }

> public CoffeeCup(int size) {
> this(size, MIN_INNER_COFFEE);
> }

> public CoffeeCup(int size, int startingAmount) {
> size = SHORT;
> innerCoffee = startingAmount;
> }

> This way, you only need to refer to the minimum innerCoffee
>value once instead of twice.
This is also OK, but in saving the programmer a bit of typing,
you make the VM do an extra constructor invocation. You also
make the programmer hop to an extra constructor definition to
find out what all the defaults will be. My preference is to
always invoke this() with the maximum arguments, but the
difference with that and your suggestion is not very
significant in this case. When you get to a constructor that
has 5 or 6 arguments, however, that gets to be a lot of extra
constructor invocations and hops of the programmer's eye.



Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us