Re: static methods in interfaces
ballpointpenthief wrote:
Hopefully this is a SSCCE:
(I have reason not to use an abstract class.)
public interface TheInterface {
public static String getSomethingReleventToClass();
}
public Class AClass implements TheInterface {
private String somethingReleventToClass = "This will be different
in each class";
public static String getSomethingReleventToClass() {
return somethingReleventToClass;
}
}
public Class Application {
private TheInterface someClass;
public Application() {
someClass.getSomethingReleventToClass();
}
}
Cheers,
Matt
Ok, notice that in your code here, you actually have an INSTANCE of
TheInterface in class Application. So, the way you are invoking
getSomethingRelevantToClass is correct, IF you were not attempting to
define getSomethingRelevantToClass statically.
From your previous emails, it sounded like you actually only wanted to
pass the class name around, or Class object, to be precise. As far as I
know, the fundamental problem with your approach is that since you
cannot define static methods on an interface, you cannot do something like:
interface TheInterface {
public static String getSomethingRelevantToClass();
}
and hope to do:
TheInterface.getSomethingRelevantToClass();
However, I think that you will find that they way you wrote it above is
99.9% correct, if you have INSTANCES of classes implementing
TheInterface, just take out the "static" when defining your TheInterface
class.
This is actually entirely standard OO programming in Java.
Rogan