Re: static methods in interfaces
ballpointpenthief wrote:
Hopefully this is a SSCCE:
Only something that compiles can be an SSCCE, but it is an SSCE (static
self-contained example).
(I have reason not to use an abstract class.)
public interface TheInterface {
public static String getSomethingReleventToClass();
Why does this need to be static?
}
public Class AClass implements TheInterface {
private String somethingReleventToClass = "This will be different
in each class";
If it is associated with the class, why isn't this static?
public static String getSomethingReleventToClass() {
return somethingReleventToClass;
}
}
public Class Application {
private TheInterface someClass;
public Application() {
someClass.getSomethingReleventToClass();
}
}
I would just make getSomethingRelevantToClass an instance method, in
both the interface and the implementation. Instance methods can
reference static variables just as well as static methods can. On other
hand, in any implementation in which somethingRelevantToClass is
correctly named, I would make it a static variable.
There can't be any problems with needing to call the method through the
interface without an object existing. Interfaces are inherently
abstract, so you can't do anything much with them other than access
constants unless you have an instance of an implementing class.
Patricia