Sometimes I write classes that can be converted to and from another, and I'm used to writing it as a non-static conversion method and a static conversion method, for example:
class A { B toB() {...} static A fromB(B b) {...} }
or
class B { void save(File f) {...} static B load(File f) {...} }
I used to think that this is a good and simple approach, but recently the static method of the conversion from me is annoying, for example, if I want to define an interface for types that can be converted to and from - B :
interface ConvertableToAndFromB { B toB();
So, is there an elegant way to do this without conversion - due to statics other than migration to Smalltalk?
EDIT
To clarify, I understand that I can add a non-static method to the interface, for example:
interface ConvertableToAndFromB { B toB(); void fromB(B b); }
or, if I want to allow immutable types (thanks to Stripling):
interface ConvertableToAndFromB<T implements ConvertibleToAndFromB<T>> { B toB(); T fromB(B b); }
But this will require me to create a new A before I can even call it, as in:
A a = new A(); a.fromB(b);
or (for immutable):
A a = new A(); a = a.fromB(b);
what I'm trying to avoid (but I will not do with another solution). I just hope it gets better.