I have an interface like this:
public interface Byteable<T> { byte[] toBytes(); T fromBytes(byte[] bytes); } which, like the name implies, transforms an object to a byte pattern and can recreate an object from a given byte-array (or throw some kind of exception if not). I like the toBytes() method and it must be non-static, because it can only be applied to objects. But how can I create a static method so that I can call
ClassName.fromBytes(...); How can I ensure, that every class implementing the interface has both of those functions or how can I create a static method that uses the non-static fromBytes(...)? Is it good practice to create a default constructor and the do something like
new ClassName().fromBytes(); Is there any good practice? I guess an abstract class isn't a solution either, because it can not have static methods, can it?
staticmethod in implementations, but why does it matter? When you want astaticfromBytesmethod inClassName, you have to declare the method inClassNameanyway, whether mandated by the interface or not. To the caller performingClassName.fromBytes(...);, it makes no difference whether an interface declares the method or not.