On 15.08.2006 16:20, Patricia Shanahan wrote:
Robert Klemme wrote:
...
If you're worried about performance writing a mutable Integer is the
best solution. Note that this class then also should inherit
java.lang.Number.
Why inherit from java.lang.Number?
I had a situation like this, and wrote the following class, inside my
Counter class which has the HashMap:
private static class Count {
private int val = 0;
private void increment() {
val++;
}
private int get() {
return val;
}
}
It does everything the surrounding class needs it to do, and not one
thing more. You seem to saying that I should have implemented several
public methods that the surrounding class will never need, and no other
class can call except through reflection?
Of course you can do it this way. But if you frequently (i.e. in
different classes) need a mutable Integer / Float then IMHO it's better
to provide a more general implementation. And if that inherits
java.lang.Number (and implements methods as needed) then it can also be
used in contexts that deal with Number so it's more generally usable.
This should probably be in the standard lib anyway.
at some point refactor my code to use it instead of my Count class.
However, so far, I've had no need for a mutable integer.